<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On May 27, 2016, at 10:37 AM, plx via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""><div class="">On May 26, 2016, at 1:00 PM, T.J. Usiyan via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">A `deriving` keyword, at the very least, is pretty explicitly *not* an all-or-nothing situation. If you want to define equality/hashability for your type manually, don't use `deriving`. This should leave the simplest cases to auto generation and anything more complex should be handled by the developer.</div></div></blockquote><div class=""><br class=""></div><div class="">It’s all-or-nothing in the sense you can’t use a naive `deriving` implementation to assist in any case where what you need is *almost* the trivial implementation, but not quite.</div><div class=""><br class=""></div><div class="">Consider a case like this:</div><div class=""><br class=""></div><div class=""> class QuxEvaluator {</div><div class=""> </div><div class=""> let foo: Foo // Equatable</div><div class=""> let bar: Bar // Equatable</div><div class=""> let baz: Baz // Equatable</div><div class=""><br class=""></div><div class=""> private var quxCache: [QuxIdentifier:Qux] // [Equatable:Equatable] = [:]</div><div class=""><br class=""></div><div class=""> // pure function of `foo`, `bar`, `baz`, and `identifier`</div><div class=""> // expensive, and uses `quxCache` for memoization </div><div class=""> func qux(for identifier: QuxIdentifier) -> Qux</div><div class=""><br class=""></div><div class=""> }</div><div class=""><br class=""></div><div class="">…if it weren’t for `quxCache` we could easily synthesize `==` for `QuxEvaluator`, but the trivial synthesis will yield invalid results due to `[QuxIdentifier:Qux]` also being `Equatable` (really: it *will* also be equatable once conditional conformances are in place).</div><div class=""><br class=""></div><div class="">So we’re back to e.g. writing this: </div><div class=""><br class=""></div><div class=""> extension QuxEvaluator : Equatable {</div><div class=""><br class=""></div><div class=""> }</div><div class=""><br class=""></div><div class=""> func ==(lhs: QuxEvaluator, rhs: QuxEvaluator) -> Bool {</div><div class=""> return (lhs === rhs) || (lhs.foo == rhs.foo && lhs.bar == rhs.bar && lhs.baz == rhs.baz)</div><div class=""> }</div><div class=""><br class=""></div><div class="">…just to omit a single field from the `==` consideration; this is another sense in which you can say deriving is an all-or-none; there’s just no way to invoke the synthesis mechanism other than for "all fields”.</div></div></div></div></blockquote><div><br class=""></div><div>I don’t see why this must necessarily be the case. Annotations such as you describe below could be taken into account by `deriving`. `deriving` is just a way to invoke the synthesis mechanism.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><div class=""><br class=""></div><div class="">On the one hand, it’s possible to imagine a finer-grained form of this synthesis that’d allow you to e.g. indicate a certain field should be omitted (and also perhaps specialize how fields are compared, customize the synthesized comparison ordering to put cheaper comparisons earlier, and an endless list of other possible requests…).</div></div></div></div></blockquote><div><br class=""></div><div>If you don’t trust the compiler to optimize this well and therefore want control over order of comparisons you should probably just implement it manually. As you note below, this is a convenience feature that needs to strike a fine balance.</div><div><br class=""></div><div>IMO there are two issues involved:</div><div><br class=""></div><div>1. How do we invoke the automatic synthesis.</div><div>2. How do we have some degree of control over the synthesis that happens.</div><div><br class=""></div><div>`deriving` addresses issue 1 and says nothing about issue 2.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><div class=""><br class=""></div><div class="">On the other hand, the memberwise-init proposal had a very similar situation: the naive approach was arguably too narrow, but it proved very difficult to find a workable balance between customization and implementation complexity (leaving it postponed for the foreseeable future); it’d be a pity if synthesis like this fell into the same trap.</div><div class=""><br class=""></div><div class="">But, on the gripping hand, I highly suspect that a naive `deriving`/synthesis will wind up being too narrowly-useful to really justify; that’s just my opinion, of course.</div><br class=""><blockquote type="cite" class=""><div class=""><div class="gmail_extra"><br class=""><div class="gmail_quote">On Thu, May 26, 2016 at 11:20 AM, L. Mihalkovic <span dir="ltr" class=""><<a href="mailto:laurent.mihalkovic@gmail.com" target="_blank" class="">laurent.mihalkovic@gmail.com</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto" class=""><div class="">what i care about is to have a choice about what DEFINES the identity of my values, not just an all-or-nothing situation.<br class=""></div><div class=""><div class="h5"><div class=""><br class="">On May 26, 2016, at 5:18 PM, T.J. Usiyan via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>> wrote:<br class=""><br class=""></div><blockquote type="cite" class=""><div class=""><div dir="ltr" class="">+1 to a `deriving` keyword</div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Thu, May 26, 2016 at 3:58 AM, Michael Peternell via swift-evolution <span dir="ltr" class=""><<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">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<br class="">
<br class="">
struct Polygon deriving Equatable, Hashable {<br class="">
...<br class="">
}<br class="">
<br class="">
This has several advantages:<br class="">
- you don't have to guess wether `Equatable` or `Hashable` should be automatically derived or not.<br class="">
- Deriving becomes an explicit choice.<br class="">
- If you need a custom `Equatable` implementation (for whatever reason), you can still do it.<br class="">
- It doesn't break any code that is unaware of the change<br class="">
- It can be extended in future versions of Swift, without introducing any new incompatibilities. For example, `CustomStringConvertible` could be derived just as easily.<br class="">
- 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.)<br class="">
- It is proven to work in production.<br class="">
<br class="">
-Michael<br class="">
<div class=""><div class=""><br class="">
> Am 26.05.2016 um 03:48 schrieb Mark Sands via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>>:<br class="">
><br class="">
> Thanks so much for putting this together, Tony! Glad I was able to be some inspiration. :^)<br class="">
><br class="">
><br class="">
> On Wed, May 25, 2016 at 1:28 PM, Tony Allevato via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>> wrote:<br class="">
> 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/" rel="noreferrer" target="_blank" class="">http://thread.gmane.org/gmane.comp.lang.swift.evolution/8919/</a>> that recently got necromanced (thanks Mark Sands!).<br class="">
><br class="">
> 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.<br class="">
><br class="">
> Automatically deriving Equatable andHashable for value types<br class="">
><br class="">
> • Proposal: SE-0000<br class="">
> • Author(s): Tony Allevato<br class="">
> • Status: Awaiting review<br class="">
> • Review manager: TBD<br class="">
> Introduction<br class="">
><br class="">
> 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.<br class="">
><br class="">
> Swift-evolution thread: Universal Equatability, Hashability, and Comparability<br class="">
><br class="">
> Motivation<br class="">
><br class="">
> 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:<br class="">
><br class="">
> func ==(lhs: Foo, rhs: Foo) -> Bool<br class="">
> {<br class="">
><br class="">
> return lhs.property1 == rhs.property1 &&<br class="">
><br class="">
> lhs<br class="">
> .property2 == rhs.property2 &&<br class="">
><br class="">
> lhs<br class="">
> .property3 == rhs.property3 &&<br class="">
><br class="">
><br class="">
> ...<br class="">
><br class="">
> }<br class="">
><br class="">
> 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.<br class="">
><br class="">
> 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.<br class="">
><br class="">
> In particular, the code that must be written to implement equality for enums is quite verbose. One such real-world example (source):<br class="">
><br class="">
> func ==(lhs: HandRank, rhs: HandRank) -> Bool<br class="">
> {<br class="">
><br class="">
> switch<br class="">
> (lhs, rhs) {<br class="">
><br class="">
> case (.straightFlush(let lRank, let lSuit), .straightFlush(let rRank , let<br class="">
> rSuit)):<br class="">
><br class="">
> return lRank == rRank && lSuit ==<br class="">
> rSuit<br class="">
><br class="">
> case (.fourOfAKind(four: let lFour), .fourOfAKind(four: let<br class="">
> rFour)):<br class="">
><br class="">
> return lFour ==<br class="">
> rFour<br class="">
><br class="">
> case (.fullHouse(three: let lThree), .fullHouse(three: let<br class="">
> rThree)):<br class="">
><br class="">
> return lThree ==<br class="">
> rThree<br class="">
><br class="">
> case (.flush(let lRank, let lSuit), .flush(let rRank, let<br class="">
> rSuit)):<br class="">
><br class="">
> return lSuit == rSuit && lRank ==<br class="">
> rRank<br class="">
><br class="">
> case (.straight(high: let lRank), .straight(high: let<br class="">
> rRank)):<br class="">
><br class="">
> return lRank ==<br class="">
> rRank<br class="">
><br class="">
> case (.threeOfAKind(three: let lRank), .threeOfAKind(three: let<br class="">
> rRank)):<br class="">
><br class="">
> return lRank ==<br class="">
> rRank<br class="">
><br class="">
> case (.twoPair(high: let lHigh, low: let lLow, highCard: let<br class="">
> lCard),<br class="">
><br class="">
> .twoPair(high: let rHigh, low: let rLow, highCard: let<br class="">
> rCard)):<br class="">
><br class="">
> return lHigh == rHigh && lLow == rLow && lCard ==<br class="">
> rCard<br class="">
><br class="">
> case (.onePair(let lPairRank, card1: let lCard1, card2: let lCard2, card3: let<br class="">
> lCard3),<br class="">
><br class="">
> .onePair(let rPairRank, card1: let rCard1, card2: let rCard2, card3: let<br class="">
> rCard3)):<br class="">
><br class="">
> return lPairRank == rPairRank && lCard1 == rCard1 && lCard2 == rCard2 && lCard3 ==<br class="">
> rCard3<br class="">
><br class="">
> case (.highCard(let lCard), .highCard(let<br class="">
> rCard)):<br class="">
><br class="">
> return lCard ==<br class="">
> rCard<br class="">
><br class="">
> default<br class="">
> :<br class="">
><br class="">
> return false<br class="">
><br class="">
> }<br class="">
> }<br class="">
><br class="">
> Crafting a high-quality hash function for this enum would be similarly inconvenient to write, involving another large switchstatement.<br class="">
><br class="">
> 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:<br class="">
><br class="">
> enum Foo: Int<br class="">
> {<br class="">
><br class="">
> case one = 1<br class="">
><br class="">
><br class="">
> case two = 2<br class="">
><br class="">
> }<br class="">
><br class="">
><br class="">
> let x = (Foo.one == Foo.two) // works<br class="">
> let y = Foo.one.hashValue // also works<br class="">
> let z = Foo.one.rawValue // also also works<br class="">
> Since there is precedent for this in Swift, we propose extending this support to more value types.<br class="">
><br class="">
> Proposed solution<br class="">
><br class="">
> 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.<br class="">
><br class="">
> Specifically, we propose the following rules for deriving Equatable:<br class="">
><br class="">
> • 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.<br class="">
><br class="">
> • 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.<br class="">
><br class="">
> Likewise, we propose the following rules for deriving Hashable:<br class="">
><br class="">
> • 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.<br class="">
><br class="">
> Since order of the terms affects the hash value computation, we recommend ordering the terms in member definition order.<br class="">
><br class="">
> • 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.<br class="">
><br class="">
> † 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.<br class="">
><br class="">
> Overriding defaults<br class="">
><br class="">
> 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.<br class="">
><br class="">
> Open questions<br class="">
><br class="">
> Omission of fields from generated computations<br class="">
><br class="">
> 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.<br class="">
><br class="">
> Explicit or implicit derivation<br class="">
><br class="">
> 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?<br class="">
><br class="">
> Impact on existing code<br class="">
><br class="">
> 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.<br class="">
><br class="">
> Alternatives considered<br class="">
><br class="">
> 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.)<br class="">
><br class="">
> Acknowledgments<br class="">
><br class="">
> 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.<br class="">
><br class="">
><br class="">
> _______________________________________________<br class="">
> swift-evolution mailing list<br class="">
> <a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class="">
> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
><br class="">
><br class="">
> _______________________________________________<br class="">
> swift-evolution mailing list<br class="">
> <a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class="">
> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
<br class="">
_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
</div></div></blockquote></div><br class=""></div>
</div></blockquote><blockquote type="cite" class=""><div class=""><span class="">_______________________________________________</span><br class=""><span class="">swift-evolution mailing list</span><br class=""><span class=""><a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a></span><br class=""><span class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br class=""></div></blockquote></div></div></div></blockquote></div><br class=""></div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>