// (automatically synthesized Hashable)
struct Pair<T: Hashable, U: Hashable>: Hashable {
let left: T
let right: U
// convenience init
init(_ left: T, _ right: U){
self.left = left
self.right = right
}
}
// โญ๏ธ ไฝฟ็จ Pair ๆ๏ผไธ้็นๅฅๆๅฎ <T, U>
let pair = Pair("Joe", 20)
let dict = [pair: "friend"]
Generic code is turned to specialized code at compile time.
Hashable - type parameter ๆ generic type ๆฌ่บซๆๆๅฟ
้ ๆฏ Hashable, ไพๅฆ๏ผ็ถไฝ Dictionary ็ Key ๆๆฏ็ถไฝ Set ็ๅ
็ด ใ
ๅ๏ผใ ๅฏไปฅๅฎ็พฉ S<A>
ๅๅฎ็พฉ S<A, B>
ๅโใ
็ญ๏ผใ ไธ่กโ๏ธ(redeclaration of S
) ใ