[swift-evolution] [Draft] Automatically deriving Equatable and Hashable for certain value types

L Mihalkovic laurent.mihalkovic at gmail.com
Mon May 30 15:15:09 CDT 2016

Seems to me like a heavy weight solution to invent a special keyword that will be applicable to 2 protocols in the entire standard library. 

If I am not mistaken, the Swift philosophy seems to be that Protocols carry the meaning, not how we apply them. So in this instance, something like the following might convey the meaning of both equality, and how the compiler should assist.

public protocol Equatable {
    public func ==(lhs: Self, rhs: Self) -> Bool

/// Declaring conformance to this protocol results in the compiler
/// automatically generating a deep-equal between each members of
/// the conforming instances
public protocol AutoDeepEquatable : Equatable {

/// Declaring conformance to this protocol results in the compiler
/// automatically generating a shallow-equal between each members 
/// of the conforming instances
public protocol AutoShallowEquatable : Equatable {

> On May 30, 2016, at 9:04 PM, Michael Peternell via swift-evolution <swift-evolution at swift.org> wrote:
> It seems that there are two groups here. 1) Group 1 wants Equatable (and others) be derived automatically unless specifically overridden: similar to auto-synthesis of properties in Objective-C. 2) The other group (Group 2) wants Equatable (and others) be derived explicitly using a `deriving` keyword (or something semantically equivalent). Unless I missed something, there were no voices for keeping the status quo, and not introducing any process to automatically derive these protocols.
> I think I chose an easy strategy when proposing the `deriving` keyword. Haskell is a mature language, and "copying" a feature from them is usually a safe choice. For each language feature, someone has to think through all the implications of it; this is usually far from trivial. I argue that if I take a feature from another language, someone has probably already thought about all the pros and cons of different solutions. This is just a plea for embracing precedent.
> There is one advantage of method 2 that (I think) hasn't been discussed so far: when you declare a type `S`, and an `Equatable` instance is automatically derived, there is no way to override that instance in another module. With method 1, there is also no way to request that an `Equatable` instance should *not* be generated. I think no one will vote for something like `struct S @notderiving(Equateble,Hashable) { ... }`.
> Also, a `deriving` keyword is less magical than just automatically deriving `Equatable` and `Hashable` instances. I think the language rules should be as simple as possible, by default. If you want any kind of special behavior, you have to ask for it: `deriving Equatable`, `@IBOutlet`, `@NSManaged`. Furthermore, I think it is good that the developer is aware that there is an "==" method somewhere, specifically for this new type. The compiler should not arbitrarily create methods, because someone may need them. Even if it is very likely that you will need them. Just like in a coffee house, you are asked if you want a coffee, even if you are visiting it every day. For example with Objective-C, I want each developer to be aware of the difference between a property and an iVar, and be aware of the connection between properties, methods, and key-value-coding. The complexities of the language shouldn't be hidden completely.
> Just my two cents..
> -Michael
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

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

More information about the swift-evolution mailing list