-
Notifications
You must be signed in to change notification settings - Fork 20
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
Shorthand syntax for single case DUs #727
Comments
I think this is a minor change with big positive impact. Thumbs up! :) |
Just to expand on this, for people new to F# I've found a common pitfall is to not fully understand the difference between a DU case and the DU type itself. Even after understanding the difference they sometimes conflate these two things for a while. This syntax blurs the boundaries between those two things which could contribute to that misunderstanding. I would also add as a distinct disadvantage that this would add yet another instance in F# where there are multiple equivalent ways to do exactly the same thing, making it harder to know what's idiomatic and harder to read code in general. I think F# already has more of these than many languages. This is made worse by being a late addition to the language, so even the most idiomatic code will never use this feature if it was written for earlier F# versions. |
at least the way I use them, single case DUs may fragment into n-cases... that's part of the beauty of using them. Visually, I don't see a clear path from the proposed shorthand to the additional cases. Stylistically, I feel it's better to be explicit on this. |
I think this syntax is better suited as a general extension of the existing syntax Bonus points if we could use the parameters inside the type directly, unlike type HttpHeader of name:string * value:string with
member __.Formatted = sprintf "%s: %s" name value Edit: note it would also subsume existing type TemperatureException of value:int with
inherit Exception(sprintf "Reached %d degrees" value) // No `this.Message` override needed! |
I'm not yet sure about the proposed syntax, as it is not too inconvenient to define a single case DU right now when needed, but it looks fitting idioms. Is this something that should go along the same line as haskell "newtype" or even related to some degree to units of measures? |
I think this shorthand is too similar to a type alias. |
|
Agreed. Using the same name for the DU type as for one of its cases is confusing. If anything, doing this should be warned about, not encouraged with a shorthand syntax. |
I can certainly sympathize with the existing syntax with single-case DUs today - it kind of sucks, and the tooling point is valid. I wouldn't shocked if it were done differently had F# been first conceived today. However, I think this does break the following design principles:
|
Closing as I'm not inclined to any new syntax for these. |
Title of Suggestion
I propose we add a shorthand syntax for single case Discriminated Unions.
The existing way of approaching this problem in F# is usually repeating the type name.
Pros and Cons
The advantages of making this adjustment to F# are
The disadvantages of making this adjustment to F# are one more syntax to learn.
Extra information
Estimated cost (XS, S, M, L, XL, XXL): S
Related suggestions: (put links to related suggestions here)
Affidavit (please submit!)
Please tick this by placing a cross in the box:
Please tick all that apply:
The text was updated successfully, but these errors were encountered: