Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expression of type T cannot be handled by a pattern of type X

I have upgraded my project to target C# 7 and used Visual Studio 2017 RC to implement pattern matching across my solution. After doing this some errors were introduced relating to pattern matching with generic parameters.

Consider the following code:

public class Packet { }  public class KeepalivePacket : Packet { }  public void Send<T>(T packet)     where T : Packet {     if (packet is KeepalivePacket keepalive)     {         // Do stuff with keepalive     }      switch (packet)     {         case KeepalivePacket keepalivePacket:             // Do stuff with keepalivePacket             break;     } } 

Both the if statement and the case statement produce a compilation error.

An expression of type T cannot be handled by a pattern of type KeepalivePacket

If I first cast the parameter to type object the pattern matching works as expected. Roslyn then marks the cast to object as redundant.

if ((object)packet is KeepalivePacket keepalive) {     // This works } 

This error only appears to apply to generic parameters and variables. Roslyn appears to not be aware of this issue as it recommends changing the code to use pattern matching via an analyzer and allows me to apply the "code fix" resulting in the broken code.

like image 668
Alex Wiese Avatar asked Jan 03 '17 04:01

Alex Wiese


2 Answers

As explained by Neal Gafter from Microsoft:

The reason it doesn’t work is that there is no conversion (explicit or implicit) defined from T to KeepalivePacket. Pattern matching requires such a conversion to exist, as it is defined in terms of the cast operator, which requires a conversion exist. The language specification and compiler agree that no conversion exists. It seems strange to me that the language specification is defined such that no (explicit) conversion exists here. We'll look at what we can do about that.

We're not going to do anything about this in C# 7. You'll have to add a cast to your code to work around it. Once we have recursive patterns, this may be more difficult to work around. Moreover, the awkward language rule that underlies this issue (i.e. that there is no conversion from T to KeepalivePacket) doesn't make a lot of sense.

Update

This is now working in C# 7.1

like image 168
Alex Wiese Avatar answered Sep 28 '22 14:09

Alex Wiese


C# 7.1 supports this now. For example, see "Pattern Matching with Generics" in this article. You may need to add <LangVersion>7.1</LangVersion> or <LangVersion>latest</LangVersion> to your project file. See here for details on configuring LangVersion.

like image 29
Nate Cook Avatar answered Sep 28 '22 15:09

Nate Cook