pattern-matching with generics
Summary
The specification for the existing C# as operator (§11.11.12) permits there to be no conversion between the type of the operand and the specified type when either is an open type. However, in C# 7 the Type identifier pattern requires there be a conversion between the type of the input and the given type.
We propose to relax this and change expression is Type identifier, in addition to being permitted in the conditions when it is permitted in C# 7, to also be permitted when expression as Type would be allowed. Specifically, the new cases are cases where the type of the expression or the specified type is an open type.
Motivation
Cases where pattern-matching should "obviously" be permitted currently fail to compile. See, for example, https://github.com/dotnet/roslyn/issues/16195.
Detailed design
We change the paragraph in the pattern-matching specification (the proposed addition is shown in bold):
Certain combinations of static type of the left-hand-side and the given type are considered incompatible and result in compile-time error. A value of static type
Eis said to be pattern compatible with the typeTif there exists an identity conversion, an implicit reference conversion, a boxing conversion, an explicit reference conversion, or an unboxing conversion fromEtoT, or if eitherEorTis an open type. It is a compile-time error if an expression of typeEis not pattern compatible with the type in a type pattern that it is matched with.
Drawbacks
None.
Alternatives
None.
Unresolved questions
None.
Design meetings
LDM considered this question and felt it was a bug-fix level change. We are treating it as a separate language feature because just making the change after the language has been released would introduce a forward incompatibility. Using the proposed change requires that the programmer specify language version 7.1.
Feedback
Issottometti u ara feedback għal