<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><br></div><div><div><br><br>Sent from my iPhone</div>On Jan 13, 2017, at 13:51, Adam Shin via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr"><div class="gmail_msg" style="font-size:13px">When using enums with associated values, it's often necessary to check for equality between two enum objects in some way. That can lead to boilerplate code like this:<br class="gmail_msg"></div><div class="gmail_msg" style="font-size:13px"><br class="gmail_msg"></div><div class="gmail_msg" style="font-size:13px">enum Option {</div><div class="gmail_msg" style="font-size:13px">&nbsp; &nbsp; case foo(String)</div><div class="gmail_msg" style="font-size:13px">&nbsp; &nbsp; case bar(Int)</div><div class="gmail_msg" style="font-size:13px"><span class="inbox-m_-1513968597867029764Apple-tab-span gmail_msg" style="white-space:pre-wrap">        </span>case zip</div><div class="gmail_msg" style="font-size:13px">}</div><div class="gmail_msg" style="font-size:13px"><br class="gmail_msg"></div><div class="gmail_msg" style="font-size:13px">func ==(lhs: Option, rhs: Option) -&gt; Bool {</div><div class="gmail_msg" style="font-size:13px">&nbsp; &nbsp; switch (lhs, rhs) {</div><div class="gmail_msg" style="font-size:13px">&nbsp; &nbsp; case (.foo(let a), .foo(let b)) where a == b: return true</div><div class="gmail_msg" style="font-size:13px">&nbsp; &nbsp; case (.bar(let a), .bar(let b)) where a == b: return true</div><div class="gmail_msg" style="font-size:13px">&nbsp; &nbsp; case (.zip, .zip): return true</div><div class="gmail_msg" style="font-size:13px">&nbsp; &nbsp; default: return false</div><div class="gmail_msg" style="font-size:13px">&nbsp; &nbsp; }</div><div class="gmail_msg" style="font-size:13px">}</div><div class="gmail_msg" style="font-size:13px"><br class="gmail_msg"></div><div class="gmail_msg" style="font-size:13px">..which results in code duplication and opens the door to potential logic errors.</div><div class="gmail_msg" style="font-size:13px"><br class="gmail_msg"></div><div class="gmail_msg" style="font-size:13px">Instead, what if enums with associated values were automatically Equatable when all their associated values were Equatable? That would remove the need for such boilerplate code.</div><div class="gmail_msg" style="font-size:13px"><br class="gmail_msg"></div><div class="gmail_msg" style="font-size:13px">The Swift language guide states that custom classes and structs don't receive a default implementation of the == operator because the compiler can't guess what "equality" means for them. However, I think this could make sense for enums. An enum case, even with associated values, seems closer to a value itself than an object with logic and state.</div><div class="gmail_msg" style="font-size:13px"><br class="gmail_msg"></div><div class="gmail_msg" style="font-size:13px">I'd be interested to hear any thoughts on this. Would this be a beneficial addition?</div></div></div></blockquote><br><div>I think it makes more sense to come up with some syntax for reducing that kind of boilerplate code in general. In "pseudo-english", I'd write the == function as "if lhs and rhs are the same case &amp;&amp; their associated values are the equal, return true, else return false".</div><div><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div><span style="background-color: rgba(255, 255, 255, 0);">What about doing something with the reflection system? Isn't that supposed to get overhauled for Swift 4? I'm not sure what the performance implications would be, though.</span></div><div><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div><span style="background-color: rgba(255, 255, 255, 0);">- Dave Sweeris&nbsp;</span></div></body></html>