Skip to content

Instantly share code, notes, and snippets.

@greenchiu
Created December 31, 2019 02:43
Show Gist options
  • Save greenchiu/19c4278dc65c30306148af34b7478d21 to your computer and use it in GitHub Desktop.
Save greenchiu/19c4278dc65c30306148af34b7478d21 to your computer and use it in GitHub Desktop.
protocol X {
func hello()
}
extension X {
func hello() {
print("Hello")
}
}
extension X where Self: A {
func hello() {
print("是在 Hello?")
}
}
extension X where Self: B {
func hello() {
print("是在 Hello 2?")
}
}
class A: X {}
class B: A {}
A().hello()
let b: X = B()
b.hello()
//是在 Hello?
//是在 Hello?
let c = B()
c.hello()
//是在 Hello 2?
@the-yuyut
Copy link

看了覺得神奇! 以後寫 extension 要小心了
一直在找 `class C` 在哪, 結果發現 `c = B()`

@greenchiu
Copy link
Author

@ytyubox 中招當下會反應不過來,踩過就知道了 orz

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