🍀generics

Swifttypecategory ⟩ generics

// (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"]

Last updated

Was this helpful?