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
API runtime support for efficient type switch #29002
Comments
That sounds reasonable to me. Some questions:
@stephentoub / @jkotas, any objections to this approach? |
I see type-based pattern matching as higher layer C# feature, around the same layer as We should extrapolate future pattern matching needs. If the pattern matching is going to need more specialized helpers like this one in future to execute efficiently, it would make sense to have these helpers in C# language runtime support library, like |
@jkotas There is nothing language-specific about the proposed API. It is simply a multi-way |
|
Ok, sounds reasonable to me. |
Unfortunately, this API is not correct, because the behavior of the |
Per @gafter's comment #29002 (comment) closing this. Please reopen if I misunderstood. |
I think you misunderstood. All my comment is saying is that it cannot be implemented outside the runtime because of a couple of special cases that only the runtime knows about. |
This issue track the same request as #12260. I believe the current consensus is that it can be done as an API in which the set of types to match are passed as generic type arguments (e.g. the elements of a tuple type). See https://github.com/gafter/TypeSwitch/blob/master/TypeSwitch/TypeSwitchDispatch.cs for a prototype that does not address the issue mentioned in #29002 (comment) |
Duplicate of #12260. Let's have just one issue tracking this feature request. |
I'd like the C# compiler to be able to compile a series of type tests, which typically occur in a pattern-matching switch statement, to something more efficient than a sequence of IL type tests, especially when the number of types being tested for is large (dotnet/roslyn#31515). I originally asked for an IL instruction to do this (https://github.com/dotnet/coreclr/issues/23241), but it was suggested that the library approach could do just as well. Some kind of custom hashtable is probably ideal.
I developed this prototype as a starting point: https://github.com/gafter/TypeSwitch/blob/master/TypeSwitch/TypeSwitchDispatch.cs
It appears to be faster than a series of type tests when there are more than about 10 types being tested in the switch. I think an API something like this should go in
System.Runtime.CompilerServices
and hopefully live in System.Runtime/corlib.[2019-04-29: To be clear, I am willing to contribute the code that appears at https://github.com/gafter/TypeSwitch/blob/master/TypeSwitch/TypeSwitchDispatch.cs to the dotnet foundation, who can release it under whatever license they/we see fit. The code was originally developed by me. -@gafter]
The text was updated successfully, but these errors were encountered: