csharplang icon indicating copy to clipboard operation
csharplang copied to clipboard

Champion "pattern-matching with generics" (C# 7.1)

Open gafter opened this issue 8 years ago • 16 comments

  • [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

gafter avatar Feb 20 '17 03:02 gafter

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 avatar Mar 11 '17 13:03 axel-habermaier

@axel-habermaier cast the switch expression to object to work around this issue.

gafter avatar Mar 12 '17 15:03 gafter

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)

alrz avatar Mar 12 '17 15:03 alrz

@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 avatar Apr 01 '17 19:04 aluanhaddad

@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 avatar Apr 01 '17 19:04 CyrusNajmabadi

@CyrusNajmabadi Thank you. I'm really glad to hear that this is likely to be resolved soon. That is excellent news!

aluanhaddad avatar Apr 01 '17 20:04 aluanhaddad

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.

CyrusNajmabadi avatar Apr 01 '17 20:04 CyrusNajmabadi

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.

aluanhaddad avatar Apr 01 '17 20:04 aluanhaddad

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 avatar Apr 18 '17 19:04 gafter

@gafter @jcouv was this implemented in 7.1 and now suppose to work in VS 2017?

shaggygi avatar Aug 27 '17 19:08 shaggygi

Yes

gafter avatar Aug 28 '17 05:08 gafter

Shouldn't it be closed, then?

orthoxerox avatar Aug 28 '17 06:08 orthoxerox

There is a remaining workitem to update the language spec to reflect this feature. That's why the issue is kept open.

jcouv avatar Aug 28 '17 07:08 jcouv

Thank you for reply

shaggygi avatar Aug 28 '17 11:08 shaggygi

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 avatar May 14 '18 07:05 reinseth

@reinseth Fixed. Thanks

jcouv avatar May 14 '18 21:05 jcouv