Created
September 1, 2017 02:49
-
-
Save omochi/078e3f3830147db6bcbe29dc9fbfc33c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol P0 {} | |
class Cat : P0 {} | |
func f0(_ x: Cat?) { | |
if case let p as P0 = x { | |
print("yes P0") | |
} else { | |
print("not P0") | |
} | |
} | |
f0(Cat()) // yes P0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Optional型それ自体はプロトコルを満たしていなくても as で判定すると内側の型に基づいて判定されてしまう
以下のコードは CustomStringConvertible でそれが起きることを避けるためのロジック
https://github.com/apple/swift/blob/0ebf95b8ac7b231636b08f863091b3eb0a7926eb/stdlib/public/core/OutputStream.swift#L355