Champion "pattern-matching with generics" (C# 7.1)
- [X] Proposal added (https://github.com/dotnet/csharplang/blob/master/proposals/csharp-7.1/generics-pattern-match.md)
- [X] Discussed in LDM (2017-03-28)
- [X] Decision in LDM
- [x] Finalized (done, rejected, inactive)
- [ ] Spec'ed
See https://github.com/dotnet/roslyn/issues/16195#issuecomment-270799191
This is a very surprising limitation. It severely limits the usefulness of pattern matching in generic code, making many of the changes I was hoping to make to my code base impossible.
@axel-habermaier cast the switch expression to object to work around this issue.
I think this is also related here (https://github.com/dotnet/roslyn/issues/17746)
void M<T>(T o) { if (o is X x) ; }
Ideally, e is T x should be valid anywhere e is T is. (excluding nullables, of course)
@gafter thanks for the work around but I really would like to have this work. I had the exact same experience as @axel-habermaier and it was very frustrating especially as the error was the direct result of applying a built in Roslyn suggested refactoring inside the implementation of a pattern matching API I had written. I realize it's probably a bug in the analyzer but that really compounded the frustration since the match expression in VS 15 Preview had actually obviated the need for the API entirely.
@aluanhaddad We discussed this in the LDM, and we believe this is an issue that definitely should be addressed. @gafter's comment was more about what you could do in the meantime to unblock yourself. We'll hopefully get this addressed in a upcoming release of VS (hopefully the next one :)).
Note: we've hit this issue in our own code, so we definitely feel your pain and understand why you'd want this fixed :)
@CyrusNajmabadi Thank you. I'm really glad to hear that this is likely to be resolved soon. That is excellent news!
I'm really glad to hear that this is likely to be resolved soon.
Yup! Though note the standard caveats about not being able to guarantee anything, and the future may change blah blah blah. But i'm crossing my fingers that this smoothly slots into the 7.1 release.
Though note the standard caveats about not being able to guarantee anything, and the future may change blah blah blah.
Indeed. Deadlines get missed. If that were a cardinal sin in our industry I probably wouldn't even have a job anymore. EDIT: This remark was intended to be humorous and taken lightly.
I have added a proposal for this, and a PR exists for it in the Roslyn repo at https://github.com/dotnet/roslyn/pull/18784.
@gafter @jcouv was this implemented in 7.1 and now suppose to work in VS 2017?
Yes
Shouldn't it be closed, then?
There is a remaining workitem to update the language spec to reflect this feature. That's why the issue is kept open.
Thank you for reply
The link to the proposal in the description is broken. Should be https://github.com/dotnet/csharplang/blob/master/proposals/csharp-7.1/generics-pattern-match.md
@reinseth Fixed. Thanks