简体   繁体   中英

How to restrict a Swift extension of a collection to a generic type?

Because tuples are not hashable in Swift, I have created a generic struct Couple to contain two elements which combined can be used as a key to a dictionary.

struct Couple<U: Hashable, V: Hashable>: Hashable {
    let u: U
    let v: V

    init( _ u: U, _ v: V ) {
        self.u = u
        self.v = v
    }
}
var dictionary: [ Couple<Int,Int> : Any ] = ...

Now, I would like to extend Dictionary using Couple generically.

extension Dictionary where Key == Couple<U: Hashable, V: Hashable>, Value == Any {
    func exampleConvertToArray() -> [ ( U, V, Any ) ] {
    }
}

The compiler complains no matter how I refer to Couple, U, V on the extension statement. If I instead add the generic to the function definition, the compiler also complains.

If the types are not generic ( extension Dictionary where Key == Couple<Int, Int>, Value == Any ), all is fine.

How can I create this generic extension?

This is possbile, but unfortunately the generic constraints have to be expressed on the members themselves, and not on the entire extension :

struct Couple<U: Hashable, V: Hashable>: Hashable {
    let u: U
    let v: V

    init( _ u: U, _ v: V ) {
        self.u = u
        self.v = v
    }
}

extension Dictionary {
    func exampleConvertToArray<U, V>() -> [(U, V, Any)] where Key == Couple<U, V> {
        self.map { (key, value) in (key.u, key.v, value) }
    }
}

let input = [
    Couple(1, 2): 3,
    Couple(4, 5): 6,
    Couple(7, 8): 9,
]

let result = input.exampleConvertToArray()
print(result)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM