Reputation: 342
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?
Upvotes: 2
Views: 308
Reputation: 63271
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)
Upvotes: 2