ジェネリックを使用したパターン一致pattern-matching with generics
- [x] が提案されています[x] Proposed
- [] プロトタイプ:[ ] Prototype:
- [] の実装:[ ] Implementation:
- [] 仕様:[ ] Specification:
まとめSummary
既存の C# as 演算子の仕様では、がオープン型である場合、オペランドの型と指定された型の間で変換を行うことはできません。The specification for the existing C# as operator permits there to be no conversion between the type of the operand and the specified type when either is an open type. ただし、C# 7 では、 Type identifier
入力の型と指定された型の間の変換が必要です。However, in C# 7 the Type identifier
pattern requires there be a conversion between the type of the input and the given type.
これは、C# 7 で許可されている expression is Type identifier
条件で許可されることに加えて、許可されるときにも許可されるようにすることをお勧めし expression as 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. 例については、「」を参照してください https://github.com/dotnet/roslyn/issues/16195 。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. 静的型の値は、
E
T
id 変換、暗黙の参照変換、ボックス変換、明示的な参照変換、またはからへのボックス化変換、またはE
T
E
T
がオープン型 である場合は、型とのパターン互換性があると言われます。A value of static typeE
is said to be pattern compatible with the typeT
if there exists an identity conversion, an implicit reference conversion, a boxing conversion, an explicit reference conversion, or an unboxing conversion fromE
toT
, or if eitherE
orT
is an open type. 型の式E
が、照合される型パターンの型と互換性のあるパターンでない場合、コンパイル時エラーになります。It is a compile-time error if an expression of typeE
is 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 はこの質問を検討し、バグ修正レベルの変更であると考えました。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. 提案された変更を使用するには、プログラマが言語バージョン7.1 を指定する必要があります。Using the proposed change requires that the programmer specify language version 7.1.