[swift-evolution] Equality of enum with payload

Slava Pestov spestov at apple.com
Fri Dec 11 10:37:10 CST 2015


This could be implemented by extending the derived conformance logic in Sema. Right now it only derives Equatable for enums without payload cases, but it would be relatively straightforward to synthesize the obvious Equatable conformance if all payloads are themselves Equatable, or tuples of Equatable types. You would then just write

extension MyEnumWithPayload : Equatable {}

Ditto for Hashable.

Slava

> On Dec 11, 2015, at 8:15 AM, Marc Knaup via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 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)
> }
> 
>  _______________________________________________
> 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/20151211/335a9dde/attachment.html>


More information about the swift-evolution mailing list