Swift Playgrounds: Arrays of User-Defined Type Conforming to Hashable Can’t Be Used in Sets
| Originator: | SlaunchaMan | ||
| Number: | rdar://27889151 | Date Originated: | 17-Aug-2016 01:51 PM |
| Status: | Duplicate/17144340/Open | Resolved: | |
| Product: | Swift Playgrounds | Product Version: | iOS version 10.0 (14A5341a) |
| Classification: | Other Bug | Reproducible: | Always |
Summary:
I’ve created a type copied and pasted from the documentation for the Hashable protocol, and trying to use arrays of them in a Set fails.
Steps to Reproduce:
1. Create a GridPoint type:
/// A point in an x-y coordinate system.
struct GridPoint {
var x: Int
var y: Int
}
2. Add an extension to make GridPoint conform to Hashable:
extension GridPoint: Hashable {
var hashValue: Int {
return x.hashValue ^ y.hashValue
}
static func == (lhs: GridPoint, rhs: GridPoint) -> Bool {
return lhs.x == rhs.x && lhs.y == rhs.y
}
}
3. Use this new type in a function that returns a Set of Arrays of GridPoints:
func routesToBottomRight(inGridOfSize size: Int) -> Set<[GridPoint]> {
return Set<[GridPoint]>()
}
Expected Results:
The above code will compile. Since GridPoint implements Hashable, [GridPoint] will implement Hashable.
Actual Results:
Error message: Type '[GridPoint]' does not conform to protocol 'Hashable'
Version:
iOS version 10.0 (14A5341a)
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!