<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=""><div class="">So to sum up all that, there would be three possibilities :</div><div class=""><b class=""><br class=""></b></div><div class=""><b class="">1 - have an equality operator automatically generated for enum with payloads</b></div><div class="">That would work only if all types used in the payload have equality operators themselves.</div><div class="">If a custom equality operator is defined, it should not be overridden by a generated version.</div><div class="">That might also end up allowing equality operator for tuples. I have not checked in the code if a payload with multiple variables is considered as a tuple, if so, it would certainly be more coherent to have the same behaviour for both.</div><div class="">The hashValue method should have a similar behaviour (that is, being automatically generated), as there are coherency rules to enforce between this method and the equality operator (same hasValue for equal values)</div><div class=""><br class=""></div><div class=""><b class="">2 - have a generated equality operator generated if the enum conforms to the Equatable protocol</b></div><div class="">That would be opt-in, and thus provide more control / more visibility to the developer.</div><div class="">That would work only if all types used in the payload have equality operator themselves.</div><div class="">That would not work for tuples, as a tuple cannot conform to a protocol.</div><div class="">The hashValue method should have a similar behaviour, that is being generated automatically only if the enum conforms to the Hashable protocol.</div><div class=""><br class=""></div><div class=""><b class="">3 - don’t do anything automatic</b></div><div class="">IE reject that proposal :-).</div><div class="">The developer is in control, but he (or she) might have to write equality method even for standard behaviour.</div><div class=""><br class=""></div><div class="">What do you think gentlemen, can you vote for one one of those ?</div><div class="">If solution 1 or 2 gets most votes, i’ll write an official proposal and open a pull request.</div><div class=""><br class=""></div><div class="">Thanks !</div><div class=""><br class=""></div><div class="">Jérôme</div><div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On 11 Dec 2015, at 22:03, Marc Knaup 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" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">Just stumbled upon the question whether enums/structs should automatically conform to<span class="Apple-converted-space"> </span><font face="monospace, monospace" class="">Equatable</font><span class="Apple-converted-space"> </span>when the conformance is declared indirectly through another protocol. This can be unexpected for the developer when declaring conformance to a protocol of another developer.<div class=""><br class=""></div><div class=""><font face="monospace, monospace" class="">protocol YourProtocol: Equatable {}</font></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class="">// implicitly declares conformance to Equatable - should equality operator be generated or not?</font></div><div class=""><font face="monospace, monospace" class="">enum MyEnum: YourProtocol {}</font></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class="">// better require explicit declaration to </font><span style="font-family: monospace, monospace;" class="">automatically create</span><font face="monospace, monospace" class=""> equality operator and avoid unexpected behavior</font></div><div class=""><font face="monospace, monospace" class="">enum MyEnum: Equatable, YourProtocol {}</font><br class=""></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class="">// same for Hashable</font></div><div class=""><span style="font-family: monospace, monospace;" class="">enum MyEnum: Hashable {} // should raise a error since Equatable isn't satisfied</span><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><span style="font-family: monospace, monospace;" class=""><br class=""></span></div><div class=""><span style="font-family: monospace, monospace;" class="">// must also be explicit to automatically create equality operator</span></div><div class=""><span style="font-family: monospace, monospace;" class="">enum MyEnum: Equatable, Hashable {}</span><span style="font-family: monospace, monospace;" class=""><br class=""></span></div></div><div class="gmail_extra" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""><div class="gmail_quote">On Fri, Dec 11, 2015 at 6:09 PM, Jonathan Hise Kaldma via swift-evolution<span class="Apple-converted-space"> </span><span dir="ltr" class=""><<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>></span><span class="Apple-converted-space"> </span>wrote:<br class=""><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;"><div dir="auto" class=""><div class="">+1</div><div class=""><br class=""></div><div class="">Would love to see this.<br class=""><br class="">/Jonathan</div><div class=""><div class="h5"><div class=""><br class="">11 dec. 2015 kl. 17:37 skrev Slava Pestov via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>>:<br class=""><br class=""></div><blockquote type="cite" class=""><div class="">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<div class=""><br class=""></div><div class="">extension MyEnumWithPayload : Equatable {}</div><div class=""><br class=""></div><div class="">Ditto for Hashable.</div><div class=""><br class=""></div><div class="">Slava</div><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Dec 11, 2015, at 8:15 AM, Marc Knaup via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>> wrote:</div><br class=""><div class=""><div dir="ltr" class="">It's a similar discussion with simple structs which could automatically be equatable.<div class="">And for Hashable.<br class=""><div class="">It's difficult to draw a line where that makes sense and where not.<div class=""><br class=""></div><div class="">In any case I'd prefer to not have recursive equality automatically.</div><div class=""><ul class=""><li class="">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.</li><li class="">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.</li><li class="">You cannot opt out of that behavior.</li></ul></div><div class="">But I agree that the current implementations of the equality operator for enums are awful.</div><div class="">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).</div><div class=""><br class=""></div><div class="">Something like this:</div><div class=""><span class=""><br class=""></span></div><div class=""><font face="monospace, monospace" class=""><span class="">protocol </span></font><span style="font-family: monospace, monospace;" class="">DefaultEquatable: Equatable {}</span></div><div class=""><span style="font-family: monospace, monospace;" class="">protocol DefaultHashable: Hashable {}</span></div><div class=""><font face="monospace, monospace" class=""><span class=""><br class=""></span></font></div><div class=""><font face="monospace, monospace" class=""><span class="">enum</span><span class=""><span class="Apple-converted-space"> </span>MyEnumWithPayload:<span class="Apple-converted-space"> </span></span><span class="">DefaultEquatable</span><span class="">,<span class="Apple-converted-space"> </span></span><span class="">DefaultHashable</span><span class=""><span class="Apple-converted-space"> </span>{<br class=""></span><span class=""> <span class="Apple-converted-space"> </span>case</span><span class=""><span class="Apple-converted-space"> </span>One(payload: String)<br class=""></span><span class=""> <span class="Apple-converted-space"> </span>case</span><span class=""><span class="Apple-converted-space"> </span>Two(payload: String)<br class="">}</span></font></div><div class=""><br class=""></div></div></div></div><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=NLTid1W7V2mxBEfr5Y3KfTntaxSmOQp5vjACZc9Eh19BsqU-2BFXIWDrf5TDlzGqZnbJpN6BCu-2BXDgamCC28lyGWZkyCLcilXTIuvHM-2FS9VbeSa3ZxY4p0LD0iTo4FDTyjrdJDzBMIyONu7TSghNBYkSSOt-2BOqtuPG4xZuZfYO8-2F68Cvw6GhthuPJTbwessHTbFKvl1NzQ4hL3R9F2WDb9Afmz7NzvNj-2FES1z8jVRAGwc-3D" alt="" width="1" height="1" border="0" style="min-height: 1px !important; width: 1px !important; border-width: 0px !important; margin: 0px !important; padding: 0px !important;" class=""><span class="Apple-converted-space"> </span>_______________________________________________<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" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=3H-2Bjq6g-2FrmhPdZznzzcOM7hJzszzEydjVMmbmBFWR95GeBzw-2F1Uioih7FVuLuHLSKKJSIoKVNSHqTqLyuh1xBb5sORmticOlumMLIiF070LI0QHr8SX-2FbEVSWBx67WG2htA-2FqQvgRC0ETX-2F2y6EhsjWdd4GiKVmqUStFFu2NfNv-2BlRDFX0lnFWucWPDCJkE4gAaIhsx8pqQH46g6ztQC6sUKkzjjwo-2FLLUhQJmh-2BLhA-3D" alt="" width="1" height="1" border="0" style="min-height: 1px !important; width: 1px !important; border-width: 0px !important; margin: 0px !important; padding: 0px !important;" class=""></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><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=6ZGE61OxINd5lLe2xYh9Ku-2BXbixWNr2nvfzp2IB1sZiSn0gUvATsUB-2BdxyFRKm5YmMYm-2FuQw2I2z3XgnRPrxClyt3GFwAOK6udR-2Flydb8CVFG4TSU-2FW7T6K2BpDmcWRcoPf7Ah4QejsyD7Gce5zD5HUdMCSw6tSzC-2Bjh7An5ohOaWBiSn62O8Z4q83eQX9c89XErVnDv11yTRescAIsIogxv1LXwRHyqdO-2FGe2713PA-3D" alt="" width="1" height="1" border="0" style="min-height: 1px !important; width: 1px !important; border-width: 0px !important; margin: 0px !important; padding: 0px !important;" class=""></div><br class="">_______________________________________________<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" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""><br class=""></blockquote></div><br class=""></div><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=ZfNDJ3c1KY40DpRPxaBpmw0PAd1TZW0h3lqFWlwcZNXArcMkePOo6y0YdojdHatl4ZwthlEZcJ3HxbF7sFUFnOdrK6L5-2BqQv-2FsQRkz063OCecx8JDvmyu6MSLFMIS-2FJ2PaN2tZAs6amh8-2B9UJKUK1KWT1NA0lpA2U6LMWoHU8pxXmlytTG5sHP28cfjuVS288FJ7DtQ5tDlEzFCGlYn4v7J00bNy9OvMCA98WaShImo-3D" alt="" width="1" height="1" border="0" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; height: 1px !important; width: 1px !important; border-width: 0px !important; margin: 0px !important; padding: 0px !important;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class=""><span class="Apple-converted-space"> </span>_______________________________________________</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">swift-evolution mailing list</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="mailto:swift-evolution@swift.org" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">swift-evolution@swift.org</a><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></div></blockquote></div><br class=""></body></html>