[swift-evolution] Universal Equatability, Hashability, and Comparability

Jose Cheyo Jimenez cheyo at masters3d.com
Fri Mar 11 11:19:11 CST 2016


> (starting a new thread by DaveA's request)
> 
> There's a definition of equality that makes sense as a default for nearly every type in our system:
> 
> - Basic types like IntNN, FloatNN, String, etc. have domain-defined equality,
> - Structs and tuples can be considered equal if their corresponding fields are equal,
> - Enums can be considered equal if they carry the same, equal payload,

+1 for Equality, Comparable and Hashable especially for enums and tuples. 

I know that equality was added for tuples but I think that enums are perfect for adding automatic generation of conformance to Equality, Comparable and Hashable. 

Take a look at this toy example of an Enum with 9 cases

boiler plate to conform to Equatable
https://github.com/exercism/xswift/blob/master/exercises/poker/PokerExample.swift#L151-L189 <https://github.com/exercism/xswift/blob/master/exercises/poker/PokerExample.swift#L151-L189>

boiler plate to conform to Comparable
https://github.com/exercism/xswift/blob/master/exercises/poker/PokerExample.swift#L191-L245 <https://github.com/exercism/xswift/blob/master/exercises/poker/PokerExample.swift#L191-L245>


> It's my feeling that Equatable and Hashable would make a lot of sense as universal operations; I'm not so sure about Comparable.
> 
> -Joe

For enums a defaultComparable protocol could just rank on the order the fields are declared:
https://github.com/exercism/xswift/blob/master/exercises/poker/PokerExample.swift#L211-L212 <https://github.com/exercism/xswift/blob/master/exercises/poker/PokerExample.swift#L211-L212>


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160311/8c40cf8a/attachment.html>


More information about the swift-evolution mailing list