[swift-evolution] Equality of enum with payload

Marc Knaup marc at knaup.koeln
Fri Dec 11 10:15:23 CST 2015


It's a similar discussion with simple structs which could automatically be
equatable.
And for Hashable.
It's difficult to draw a line where that makes sense and where not.

In any case I'd prefer to not have recursive equality automatically.

   - It can easily lead to unexpected behavior when you forget to implement
   an own equality operator in order to remove variables from the equation
   which do not affect the equality.
   - You could add another variable to an existing enum (or struct) which
   is now automatically compared for equality without you noticing and which
   might be wrong.
   - You cannot opt out of that behavior.

But I agree that the current implementations of the equality operator for
enums are awful.
Maybe we can make the enum (or struct) conform to a special protocol which
allows it to automatically generate the equality operator (and even default
hashValue).

Something like this:

protocol DefaultEquatable: Equatable {}
protocol DefaultHashable: Hashable {}

enum MyEnumWithPayload: DefaultEquatable, DefaultHashable {
    case One(payload: String)
    case Two(payload: String)
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151211/fa200624/attachment.html>


More information about the swift-evolution mailing list