1
のタプルと一致することはできません、次のコードを考えてみましょう:遊び場でこれを実行するは、パターンのサブタイプ
protocol P {}
struct A: P {}
func match(_ l: P, _ r: P) {
switch l {
case is A:
print("l is A")
default:
print("failed to match single value")
}
switch (l, r) {
case is (A, A):
print("(l, r) is (A, A)")
case (_, _) as (A, A):
print("(l, r) is (A, A)")
default:
print("failed to match tuple")
}
}
match(A(), A())
は、次の出力を生成します。
l is A
failed to match tuple
どうやらサブタイプのタプルをパターンマッチングが動作しません。これはバグか機能ですか?後者の場合は、理由を知ることは面白いでしょう。
関連:[Swiftの "Tuple" upcasting](http://stackoverflow.com/questions/31270507/tuple-upcasting-in-swift) – Hamish