-
-
Save koher/e3886feed159599f184be5f6328ee5e1 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 Addable { | |
associatedtype SomeType | |
static func + (a: Self, b: Self) -> Self | |
func sayHello() | |
//static func sayHello2(_ x: Self) -> Self | |
} | |
extension Addable { | |
func sayHello() { | |
print("hello<default>") | |
} | |
static func sayHello2(_ x: Self) -> Self { | |
print("hello<default>") | |
return x | |
} | |
} | |
func +<T: Addable>(a: T, b: T) -> T { | |
print("add<default>") | |
return a | |
} | |
extension Addable where SomeType == Int { | |
func sayHello() { | |
print("hello<Int>") | |
} | |
static func sayHello2(_ x: Self) -> Self { | |
print("hello<Int>") | |
return x | |
} | |
} | |
func +<T: Addable>(a: T, b: T) -> T where T.SomeType == Int { | |
print("add<Int>") | |
return a | |
} | |
func foo<T: Addable>(_ x: T, _ y: T) -> T { | |
print("foo<default>") | |
return x | |
} | |
func foo<T: Addable>(_ x: T, _ y: T) -> T where T.SomeType == Int { | |
print("foo<Int>") | |
return x | |
} | |
func bar<T: Addable>(_ x: T, _ f: (T, T) -> T) -> T { | |
return f(x, x) | |
} | |
struct Concrete<T>: Addable { | |
typealias SomeType = T | |
} | |
let a = Concrete<Int>() | |
_ = a + a // prints add<default> ... why? | |
a.sayHello() // prints hello<Int> | |
_ = Concrete.sayHello2(a) // prints hello<Int> | |
_ = foo(a, a) // prints foo<Int> | |
_ = bar(a, +) // prints add<default> | |
_ = bar(a, foo) // prints foo<Int> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment