-
Notifications
You must be signed in to change notification settings - Fork 4k
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 based on generic type #178
Comments
@Pilchie The switch expression in this proposal is switching on a generic type parameter, not an expression. What is proposed is not covered by the existing proposal. |
@colin-jack Does #206 cover the use cases this is intended to address? |
This example doesn't make sense. You are matching type parameter itself and then use private Target Map(object mappingFrom) => mappingFrom switch(
case Foo foo: new FooMapper().Map(foo),
case Bar bar: new BarMapper().Map(bar),
case *: throw new InvalidOprationException()
); |
I'm not sure I follow, you say the example doesn't make sense but your issues seems to just be with the specific syntax. I'll thus assume it is the syntax that's the issue... I'm not sure I really follow what you mean about matching type parameter. On generics though, yup it doesn't need to be generic in that case. However at time I logged the issue there was no clear syntax for any of this and for the single example I chose to make the method generic just in case you wanted to apply additional constraints. I've not thought about that aspect much though. Your example looks odd to my eyes, I understand it but compared to the Scala matching approach I find it a bit ugly. I haven't kept up with what's being suggested though, so if that switch approach is what's chosen then I'd learn to live with it. |
@colin-jack I'm not proposing any new syntax, this is the current syntax mentioned in the spec draft. So nothing with your syntax or mine. the problem is with this line |
@alrz Get you on syntax, its a pity some of the linked to pages don't have more examples as it'd help make them more accessible. Anyway your example meets my needs so when its supported I'll be laughing, hurrah. |
@colin-jack you would be able to use generics and constraints with no doubt, but you should switch on the parameter itself rather than its type. with constraints I presume your patterns also need to be pattern compatible with the resultant constrainted type. |
@alrz |
@colin-jack Since the existing form of pattern-matching addresses your use case, we're not going to take this suggestion to address your use case with pattern-matching on the type parameter. |
One use for pattern matching could be to choose appropriate code path based on a generic type. Say we want to map objects of types Foo and Bar to a new object of type Target. However the code that needs to do the mapping doesn't know if we have a Foo, a Bar, or something else:
There are alternatives already but I think this Scala style matching shows whats happening quite cleanly. Obviously you could add type constraints too:
The text was updated successfully, but these errors were encountered: