Open roboz0r opened 2 years ago
Hi @roboz0r! Your suggestion makes sense but not sure it's going to be easy to implement. Right now Fable doesn't touch F# files at all, this is left to other tools like IDEs or Myriad. Adding this capability to Fable won't be trivial and I assume it would likely conflict with the file watcher.
My thought was to somehow tell the compiler that a DU tagged with StringEnum
was compatible with string
rather than physically inserting static member inline op_Implicit (x: MyType ) : string = !!x
in the file.
As I think about it more, maybe that's not so simple and would require promoting StringEnum
to FSharp.Core
to make the main F# compiler aware of it to not generate type errors.
If it's a lot of work, it's probably not worth making the change as adding the line is trivial where needed.
Yes, unfortunately, this is not easily done. There are two main reasons:
Description
Suggestion to auto-generate implicit conversion operator for DU's with a
StringEnum
Repro code
Expected and actual results
Often working with API's which expect a
string
you will create aStringEnum
to match suitable values for your project. To use the API you then need to coerce DU back to a stringsomeFunc(string MyString)
orsomeFunc(!!MyString)
even though it gets erased anyway. Generating an implicit auto-conversion back tostring
at relevant call sites seems like an easy change without loss of type safety since the callee expects anystring
anyway.