-
Notifications
You must be signed in to change notification settings - Fork 22
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
Enable pattern matching on more specific types from type test patterns #1025
Comments
Related to #751 (comment) |
I don't think F# should add features to make type tests easier. They should not be part of the standard toolkit. |
I believe that this would complement #538 quite nicely. Now that direct type unions are allowed, we should be able to match on them more effectively. |
For me this is more about language uniformity. It's reasonable to make these things more orthogonal |
Indeed |
Also there is the inconsistency that |
Draft RFC here: fsharp/fslang-design#595 |
Thanks, marked as approved in principle |
I'd love if this could just become | DU (x, y) -> printfn $"A DU: {x}, {y}" |
@chkn The problem is with the change in type inference though. let f = function DU (x, y) -> printfn $"A DU: {x}, {y}" is inferred to be val f : _arg1:DU -> unit While changing to that would be val f : _arg1:obj -> unit With #968, you can use let f = function :? DU (x, y) -> printfn $"A DU: {x}, {y}" instead, which will have the correct type inference. |
When the input is an arbitrary object of type |
Fair enough- makes sense in this case. But perhaps we could allow for an implicit type check when the input is already known to be an anonymous union containing the type suggested by the pattern? |
@dsyme The link points to 404. |
It's been moved to the preview folder. |
Yes, I'm able to find it, thanks. The problem is many of the RFC links become 404 due to being moved. Maybe we could find an approach that would preserve the links somehow? |
Fixed thanks Yes it's annoying but github doesn't give us any way, we'd have to publish a site |
related or very close suggestion: #830 |
Implemented in F# 6 |
Enable pattern matching on more specific types from type test patterns
I propose we allow:
The existing way of approaching this problem in F# is to have an extra level of
match
expressions.Pros and Cons
The advantages of making this adjustment to F# are
The disadvantage of making this adjustment to F# is that this makes inheritance-based code easier.
Extra information
Estimated cost (XS, S, M, L, XL, XXL): S to M
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:
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: