[swift-users] Extend int arrays to be hashable?

Jean-Denis Muys jdmuys at gmail.com
Tue Sep 6 02:38:57 CDT 2016


Hello,

I am struggling to convince Swift 3 (from Xcode 8 beta 6) to let me use an array of Int as dictionary keys.

It doesn’t work out of the box, because an array is not hashable:

var answers: [[Int]:Bool] = [:] // error: Type [Int] does not conform to Protocol Hashable

One way to work around that would be to use a string representation of the Int array but this is inefficient, and besides, String implements hashValue in a similar way (I suppose) that I can do for an Int Array.

I can extend Array to include a hash function:

extension Array where Element: Int {
    
    var hashValue: Int {
        return self.reduce(5381) {
            ($0 << 5) &+ $0 &+ $1
        }
    }
}

However, Swift naturally still complains: 

var answers: [[Int]:Bool] = [:] // error: Type [Int] does not conform to Protocol Hashable

My problem is that I cannot add conformance to Hashable. The following attempt fails:

extension Array where Element: Int { // error: extension of type ‘Array’ with constraints cannot have an inheritance clause
    
    var hashValue: Int {
        return self.reduce(5381) {
            ($0 << 5) &+ $0 &+ $1
        }
    }
}

Is there a solution?

I also tried with no success to define a new confirming type as a subtype of Array<Int>:

struct HashableIntArray: Array<Int>, Hashable { // error: inheritance from non-protocol type Array<Int>
	// ...
}


I suppose I could make my HashableIntArray “have-a” Array<Int>, but then I would have to reimplement all the Array API with boilerplate code, which is really inelegant at best.

What did I miss?

Jean-Denis


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160906/803937de/attachment.html>


More information about the swift-users mailing list