-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Suggestion] String-backed enums in F# #1343
Comments
The CLR does not support this, so these would not be true enums.
Perhaps I live in a bubble, but I don't think it's particularly common outside of JS (which is why Fable supports attribute-based string enums). Why don't you use a regular DU and use |
I would say that until such a time that C# and the CLR choose to adopt the feature, it shouldn't land in F#: dotnet/csharplang#2849 |
The problem with simple Enum.ToString is:
So def doable but I don't think it's as good as native StrEnums cc @kerams |
If one is looking for a practical ways to handle enums with string values without language level changes/support in the interim, TypeShape's UnionEncoder lets you (if you use a DU) have a default mapping and override some cases via FsCodec does some wrapping of that and also has stuff like https://github.com/jet/fscodec?tab=readme-ov-file#typesafeenum-fallback-converters-using-jsonisomorphism |
Myriad may also be relevant: https://moiraesoftware.github.io/myriad/docs/plugins/du-extensions/ |
It's a very common task in backend as well, first - whenever you need to implement any protocol with values of constant strings, second - when you want to keep enum values in database, it's much better to keep strings there and not to be afraid of refactoring (I'm referring to .ToString() solution)
Fully agree |
When you mention enums, are you suggesting they should inherit the same characteristic with
The initial proposal for C# dotnet/csharplang#2849 indeed wants that behavior:
This implies that a |
@nodakai I think the idea behind enums is that we don't want 2 implementations of the same thing (C# and F#) that differ with only characteristic - handling of non-defined cases. Once C# adds that, F# users will still face the need of handling such cases. I would imagine that for F# we could add generic mechanism for any enum, like attribute |
Wouldn't literal types and erased union types void the need for this? |
They would, however literal types suggestion is not even approved |
Sure, I think it would become a lot easier to add this feature atop, once the erased union types have landed. I can't see a good reason for this particular approach or at least the examples aren't that convincing enough as an alternative to literal types + erased union. type MyStrEnum =
| A = "a-id"
| B = "b-id"
| C = "c-id" Here the strings are self descriptive and the specifying the type name in the case is moot? |
Probably something you could gen up with myriad tbh. type Stuff =
| A
| B
| C
with override this.ToString() =
match this with
| A -> "a-id"
| B -> "b-id"
| C -> "c-id"
module Stuff =
let ofStr = function | "a-id" -> Ok A | "b-id" -> Ok B | "c-id" -> Ok C | _ -> Error "unsupported string" |
I propose we add string-backed enums / string enums
The existing way of approaching this problem in F# is ...
Here is the best pattern I've been able to come up with so far. It works fine but is a bit of boilerplate to setup for a relatively common occurrence.
(lmk if you have a better soln - I am in the market for a simpler one!)
More details in: String-backed Enums in F#
Pros and Cons
The advantages of making this adjustment to F# are ...
The disadvantages of making this adjustment to F# are ...
Extra information
Estimated cost (XS, S, M, L, XL, XXL): idk
Related suggestions: (put links to related suggestions here)
Affidavit (please submit!)
Please tick these items by placing a cross in the box:
Please tick all that apply:
For Readers
If you would like to see this issue implemented, please click the 👍 emoji on this issue. These counts are used to generally order the suggestions by engagement.
The text was updated successfully, but these errors were encountered: