<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><br><br><span style="background-color: rgba(255, 255, 255, 0);">I wonder if <b>synthesizes</b> would be a better choice than <b>deriving</b>. </span></div><div id="AppleMailSignature"><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div id="AppleMailSignature"><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div><br>On May 26, 2016, at 5:58 AM, Michael Peternell via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br><br></div><blockquote type="cite"><div><span>Can we just copy&paste the solution from Haskell instead of creating our own? It's just better in every aspect. Deriving `Equatable` and `Hashable` would become</span><br><span></span><br><span>struct Polygon deriving Equatable, Hashable {</span><br><span> ...</span><br><span>}</span><br><span></span><br><span>This has several advantages:</span><br><span>- you don't have to guess wether `Equatable` or `Hashable` should be automatically derived or not.</span><br><span>- Deriving becomes an explicit choice.</span><br><span>- If you need a custom `Equatable` implementation (for whatever reason), you can still do it.</span><br><span>- It doesn't break any code that is unaware of the change</span><br><span>- It can be extended in future versions of Swift, without introducing any new incompatibilities. For example, `CustomStringConvertible` could be derived just as easily.</span><br><span>- It is compatible with generics. E.g. `struct Shape<T> deriving Equatable` will make every `Shape<X>` equatable if `X` is equatable. But if `X` is not equatable, `Shape<X>` can be used as well. (Unless `X` is not used, in which case every `Shape<T>` would be equatable. Unless something in the definition of `Shape` makes deriving `Equatable` impossible => this produces an error.)</span><br><span>- It is proven to work in production.</span><br><span></span><br><span>-Michael</span><br><span></span><br><blockquote type="cite"><span>Am 26.05.2016 um 03:48 schrieb Mark Sands via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>>:</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Thanks so much for putting this together, Tony! Glad I was able to be some inspiration. :^)</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>On Wed, May 25, 2016 at 1:28 PM, Tony Allevato via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:</span><br></blockquote><blockquote type="cite"><span>I was inspired to put together a draft proposal based on an older discussion in the Universal Equality, Hashability, and Comparability thread <<a href="http://thread.gmane.org/gmane.comp.lang.swift.evolution/8919/">http://thread.gmane.org/gmane.comp.lang.swift.evolution/8919/</a>> that recently got necromanced (thanks Mark Sands!).</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>I'm guessing that this would be a significant enough change that it's not possible for the Swift 3 timeline, but it's something that would benefit enough people that I want to make sure the discussion stays alive. If there are enough good feelings about it, I'll move it from my gist into an actual proposal PR.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Automatically deriving Equatable andHashable for value types</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span> • Proposal: SE-0000</span><br></blockquote><blockquote type="cite"><span> • Author(s): Tony Allevato</span><br></blockquote><blockquote type="cite"><span> • Status: Awaiting review</span><br></blockquote><blockquote type="cite"><span> • Review manager: TBD</span><br></blockquote><blockquote type="cite"><span>Introduction</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Value types are prevalent throughout the Swift language, and we encourage developers to think in those terms when writing their own types. Frequently, developers find themselves writing large amounts of boilerplate code to support equatability and hashability of value types. This proposal offers a way for the compiler to automatically derive conformance toEquatable and Hashable to reduce this boilerplate, in a subset of scenarios where generating the correct implementation is likely to be possible.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Swift-evolution thread: Universal Equatability, Hashability, and Comparability</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Motivation</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Building robust value types in Swift can involve writing significant boilerplate code to support concepts of hashability and equatability. Equality is pervasive across many value types, and for each one users must implement the == operator such that it performs a fairly rote memberwise equality test. As an example, an equality test for a struct looks fairly uninteresting:</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>func ==(lhs: Foo, rhs: Foo) -> Bool</span><br></blockquote><blockquote type="cite"><span> {</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>return lhs.property1 == rhs.property1 &&</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span> lhs</span><br></blockquote><blockquote type="cite"><span>.property2 == rhs.property2 &&</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span> lhs</span><br></blockquote><blockquote type="cite"><span>.property3 == rhs.property3 &&</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>...</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>}</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>What's worse is that this operator must be updated if any properties are added, removed, or changed, and since it must be manually written, it's possible to get it wrong, either by omission or typographical error.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Likewise, hashability is necessary when one wishes to store a value type in a Set or use one as a multi-valuedDictionary key. Writing high-quality, well-distributed hash functions is not trivial so developers may not put a great deal of thought into them – especially as the number of properties increases – not realizing that their performance could potentially suffer as a result. And as with equality, writing it manually means there is the potential to get it wrong.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>In particular, the code that must be written to implement equality for enums is quite verbose. One such real-world example (source):</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>func ==(lhs: HandRank, rhs: HandRank) -> Bool</span><br></blockquote><blockquote type="cite"><span> {</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>switch</span><br></blockquote><blockquote type="cite"><span> (lhs, rhs) {</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>case (.straightFlush(let lRank, let lSuit), .straightFlush(let rRank , let</span><br></blockquote><blockquote type="cite"><span> rSuit)):</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>return lRank == rRank && lSuit ==</span><br></blockquote><blockquote type="cite"><span> rSuit</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>case (.fourOfAKind(four: let lFour), .fourOfAKind(four: let</span><br></blockquote><blockquote type="cite"><span> rFour)):</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>return lFour ==</span><br></blockquote><blockquote type="cite"><span> rFour</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>case (.fullHouse(three: let lThree), .fullHouse(three: let</span><br></blockquote><blockquote type="cite"><span> rThree)):</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>return lThree ==</span><br></blockquote><blockquote type="cite"><span> rThree</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>case (.flush(let lRank, let lSuit), .flush(let rRank, let</span><br></blockquote><blockquote type="cite"><span> rSuit)):</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>return lSuit == rSuit && lRank ==</span><br></blockquote><blockquote type="cite"><span> rRank</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>case (.straight(high: let lRank), .straight(high: let</span><br></blockquote><blockquote type="cite"><span> rRank)):</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>return lRank ==</span><br></blockquote><blockquote type="cite"><span> rRank</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>case (.threeOfAKind(three: let lRank), .threeOfAKind(three: let</span><br></blockquote><blockquote type="cite"><span> rRank)):</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>return lRank ==</span><br></blockquote><blockquote type="cite"><span> rRank</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>case (.twoPair(high: let lHigh, low: let lLow, highCard: let</span><br></blockquote><blockquote type="cite"><span> lCard),</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>.twoPair(high: let rHigh, low: let rLow, highCard: let</span><br></blockquote><blockquote type="cite"><span> rCard)):</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>return lHigh == rHigh && lLow == rLow && lCard ==</span><br></blockquote><blockquote type="cite"><span> rCard</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>case (.onePair(let lPairRank, card1: let lCard1, card2: let lCard2, card3: let</span><br></blockquote><blockquote type="cite"><span> lCard3),</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>.onePair(let rPairRank, card1: let rCard1, card2: let rCard2, card3: let</span><br></blockquote><blockquote type="cite"><span> rCard3)):</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>return lPairRank == rPairRank && lCard1 == rCard1 && lCard2 == rCard2 && lCard3 ==</span><br></blockquote><blockquote type="cite"><span> rCard3</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>case (.highCard(let lCard), .highCard(let</span><br></blockquote><blockquote type="cite"><span> rCard)):</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>return lCard ==</span><br></blockquote><blockquote type="cite"><span> rCard</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>default</span><br></blockquote><blockquote type="cite"><span>:</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>return false</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span> }</span><br></blockquote><blockquote type="cite"><span>}</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Crafting a high-quality hash function for this enum would be similarly inconvenient to write, involving another large switchstatement.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Swift already provides implicit protocol conformance in some cases; notably, enums with raw values conform toRawRepresentable, Equatable, and Hashable without the user explicitly declaring them:</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>enum Foo: Int</span><br></blockquote><blockquote type="cite"><span> {</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>case one = 1</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>case two = 2</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>}</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>let x = (Foo.one == Foo.two) // works</span><br></blockquote><blockquote type="cite"><span>let y = Foo.one.hashValue // also works</span><br></blockquote><blockquote type="cite"><span>let z = Foo.one.rawValue // also also works</span><br></blockquote><blockquote type="cite"><span>Since there is precedent for this in Swift, we propose extending this support to more value types.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Proposed solution</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>We propose that a value type be Equatable/Hashable if all of its members are Equatable/Hashable, with the result for the outer type being composed from its members.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Specifically, we propose the following rules for deriving Equatable:</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span> • A struct implicitly conforms to Equatable if all of its fields are of types that conform to Equatable – either explicitly, or implicitly by the application of these rules. The compiler will generate an implementation of ==(lhs: T, rhs: T)that returns true if and only if lhs.x == rhs.x for all fields x in T.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span> • An enum implicitly conforms to Equatable if all of its associated values across all of its cases are of types that conform to Equatable – either explicitly, or implicitly by the application of these rules. The compiler will generate an implementation of ==(lhs: T, rhs: T) that returns true if and only if lhs and rhs are the same case and have payloads that are memberwise-equal.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Likewise, we propose the following rules for deriving Hashable:</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span> • A struct implicitly conforms to Hashable if all of its fields are of types that conform to Hashable – either explicitly, or implicitly by the application of these rules. The compiler will generate an implementation of hashValue that uses a pre-defined hash function† to compute the hash value of the struct from the hash values of its members.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Since order of the terms affects the hash value computation, we recommend ordering the terms in member definition order.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span> • An enum implicitly conforms to Hashable if all of its associated values across all of its cases are of types that conform to Hashable – either explicitly, or implicitly by the application of these rules. The compiler will generate an implementation of hashValue that uses a pre-defined hash function† to compute the hash value of an enum value by using the case's ordinal (i.e., definition order) followed by the hash values of its associated values as its terms, also in definition order.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>† We leave the exact definition of the hash function unspecified here; a multiplicative hash function such as Kernighan and Ritchie or Bernstein is easy to implement, but we do not rule out other possibilities.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Overriding defaults</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Any user-provided implementations of == or hashValue should override the default implementations that would be provided by the compiler. This is already possible today with raw-value enums so the same behavior should be extended to other value types that are made to implicitly conform to these protocols.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Open questions</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Omission of fields from generated computations</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Should it be possible to easily omit certain properties from automatically generated equality tests or hash value computation? This could be valuable, for example, if a property is merely used as an internal cache and does not actually contribute to the "value" of the instance. Under the rules above, if this cached value was equatable, a user would have to override == and hashValue and provide their own implementations to ignore it. If there is significant evidence that this pattern is common and useful, we could consider adding a custom attribute, such as @transient, that would omit the property from the generated computations.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Explicit or implicit derivation</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>As with raw-value enums today, should the derived conformance be completely explicit, or should users have to explicitly list conformance with Equatable and Hashable in order for the compiler to generate the derived implementation?</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Impact on existing code</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>This change will have no impact on existing code because it is purely additive. Value types that already provide custom implementations of == or hashValue but satisfy the rules above would keep the custom implementation because it would override the compiler-provided default.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Alternatives considered</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>The original discussion thread also included Comparable as a candidate for automatic generation. Unlike equatability and hashability, however, comparability requires an ordering among the members being compared. Automatically using the definition order here might be too surprising for users, but worse, it also means that reordering properties in the source code changes the code's behavior at runtime. (This is true for hashability as well if a multiplicative hash function is used, but hash values are not intended to be persistent and reordering the terms does not produce a significant behavioral change.)</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Acknowledgments</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Thanks to Joe Groff for spinning off the original discussion thread, Jose Cheyo Jimenez for providing great real-world examples of boilerplate needed to support equatability for some value types, and to Mark Sands for necromancing the swift-evolution thread that convinced me to write this up.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>_______________________________________________</span><br></blockquote><blockquote type="cite"><span>swift-evolution mailing list</span><br></blockquote><blockquote type="cite"><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br></blockquote><blockquote type="cite"><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>_______________________________________________</span><br></blockquote><blockquote type="cite"><span>swift-evolution mailing list</span><br></blockquote><blockquote type="cite"><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br></blockquote><blockquote type="cite"><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></blockquote><span></span><br><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></body></html>