-
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
Predefined value cases in discriminated unions #1051
Comments
I don't understand why FS0025 should be considered a problem rather than the language feature it actually is. Nor do I see what that has to do with the feature proposal. I can't see how this will solve problems or ease coding to the extent that the feature is justified, compared to just exploiting other features of the language. I don't see that the Pros are there, compared to what we already have. |
This come to my mind while I wrote this proposal that this warning comes up. This is one side of the medal. |
Rather than introducing new feature into the language you'd better use ActivePatterns that solves exactly you problem. |
I find this totally uncluttered to have an active pattern for each choice (~20 pcs) e.g.:
and:
Furthermore, I have a discarded parameter ( Yes, this works with active patterns, but the code looks not so nice anymore. Perhaps, it's caused that I don't like active patterns by their syntax. Better have:
|
Okay but what should happen when the value doesn't match any of your type's patterns then? Presumably an exception; but that's already what happens! In this case I think what you're really asking is:
which is 2 quite separate suggestions. |
Another solution would be to allow active patterns to have more than 7 cases. As a bonus it would be extremely cool if it had a backing type that matched the name of the active pattern "names" for fully described aka complete active patterns. That way you could store the result instead of computing it every match. |
This addition isn't the kind of thing we're going to add to F# union type definitions. There may be some kind of alternative suggestion lurking here for adhoc unions including singleton values like strings, similar to typescript, however that would be better tracked in a separate suggestion |
@dsyme Thanks for going through these! |
I propose we can add predefined cases in DUs.
I have noticed following problem in e.g. a console application:
You get a warning (FS0025) that indicates that any other cases are not covered by the pattern:
FS0025: Incomplete pattern matches on this expression. For example, the value 'aa' may indicate a case not covered by the pattern(s).
You have to create a case above e.g.:
| _ -> printfn "Command not found"
Now to my proposal:
The cases in the example above could be outsourced like:
Then you can use the case like a normal DU case:
I thought of something other to cover all cases in a pattern:
Here we have covered all cases in this pattern and we are clean as in a discriminated unions.
As in a DU case, you can use the matched value in the code block that follows (3rd & 4th case) like in a usual match-expression.
Perhaps, all pattern matching case options should be possible as shown in the F# reference.
The existing way of approaching this problem in F# is to just write down all cases in pattern matches. Perhaps more often than we actually want.
Pros and Cons
Advantage of these changes are for example:
| Key | Another -> someCode()
)Disadvantages does not come in mind. May have to be discussed.
Extra information
Estimated cost (XS, S, M, L, XL, XXL): S-M
Affidavit (please submit!)
Please tick this 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.
In the end I notice, that this proposal feels like a mix of DUs, enum types and a bit of active patterns. Maybe more steroids in enums. 😄
The text was updated successfully, but these errors were encountered: