Skip to content

Instantly share code, notes, and snippets.

@omochi
Created September 1, 2017 02:49
Show Gist options
  • Save omochi/078e3f3830147db6bcbe29dc9fbfc33c to your computer and use it in GitHub Desktop.
Save omochi/078e3f3830147db6bcbe29dc9fbfc33c to your computer and use it in GitHub Desktop.
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
@omochi
Copy link
Author

omochi commented Sep 1, 2017

Optional型それ自体はプロトコルを満たしていなくても as で判定すると内側の型に基づいて判定されてしまう

以下のコードは CustomStringConvertible でそれが起きることを避けるためのロジック
https://github.com/apple/swift/blob/0ebf95b8ac7b231636b08f863091b3eb0a7926eb/stdlib/public/core/OutputStream.swift#L355

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment