-
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
Pattern matching with Generics #828
Comments
I would consider (at least outside the compiler) matching on types and downcasting to be a code smell, and the extra difficulty of using if-statements on type objects here may provide a good deterrent. Do others consider this type of code to be proper? |
Matching on DU-like hierarchies from C# is a valid scenario and this proposal can help create concise and correct code. |
OK that's a good example! |
There is a workaround:
|
Note that the compiler can do this statically, but it requires a hidden switch (I forgot which it was), that would allow you to match over types in an SRTP way. An example is the current The simpler workaround is to just use boxing. |
Isn't it the switch that's used to compile the core library? i.e. the one that's used to make the (+) operator work with all number types |
@jwosty, yes, I think so, though the way I understand it, it's essentially an extension to SRTP. One option would be to allow that syntax outside the compiler, but I've experimented in the past with that hidden compiler switch. At least it used to work, but had some limitations. |
This is by design, as 'T is not known to be related to the target type, except via a super-type. Since any type is related to any other type via the super-type |
Pattern matching with Generics
I propose we permit pattern matching on generic types.
The existing way of approaching this problem in F# is to change it to an if-statement on type objects.
This was already implemented in C# 7.1. We should do so too.
Pros and Cons
The advantages of making this adjustment to F# are
The disadvantages of making this adjustment to F# are none that I can think of.
Extra information
Estimated cost (XS, S, M, L, XL, XXL): S
Related suggestions:
dotnet/roslyn#16195
dotnet/roslyn#16993
dotnet/csharplang#154
https://github.com/dotnet/csharplang/blob/master/proposals/csharp-7.1/generics-pattern-match.md
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: