Yeah, this is definitely cumbersome today. Fortunately I think this falls under the plans to improve Generics on the 3.0 milestone. Namely, the ability to make Array conform to Equatable when Array.Element is itself Equatable. This should make things a lot nicer :)<br><div class="gmail_quote"><div dir="ltr">On Thu, Dec 10, 2015 at 6:00 PM Jake Heiser via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Hi all!</div><div><br></div><div>Currently in Swift, checking the equality of two optional arrays is much messier than it should be:</div><div><br></div><div><div>switch (optionalArray1, optionalArray2) {</div><div>    case (.Some(let a1), .Some(let a2)) where a1 == a2: return true</div><div>    case (.None, .None): return true</div><div>    default: return false</div><div>}</div></div><div><br></div><div>This is a result of the lack of a built-in overload of == for optional arrays (and optional array slices for that matter). Overloads exist for optional equatable objects and for non-optional arrays with equatable elements, but not for optional arrays with equatable elements. The standard library currently has these overloads (among others):</div><div><br></div><div>public func ==&lt;T : Equatable&gt;(lhs: T?, rhs: T?) -&gt; Bool</div><div>public func ==&lt;Element : Equatable&gt;(lhs: ArraySlice&lt;Element&gt;, rhs: ArraySlice&lt;Element&gt;) -&gt; Bool</div><div>public func ==&lt;Element : Equatable&gt;(lhs: [Element], rhs: [Element]) -&gt; Bool</div><div><br></div><div>To correct the issue, I propose two functions be added to the standard library:</div><div><br></div><div>public func ==&lt;Element : Equatable&gt;(lhs: ArraySlice&lt;Element&gt;?, rhs: ArraySlice&lt;Element&gt;?) -&gt; Bool</div><div><div>public func ==&lt;Element : Equatable&gt;(lhs: [Element]?, rhs: [Element]?) -&gt; Bool</div></div><div><br></div><div>This change would allow optional arrays to be checked for equality just as other objects are:</div><div><br></div><div>optionalString1 == optionalString2 // Already possible</div><div>requiredArray1 == requiredArray2 // Already possible</div><div>optionalArray1 == optionalArray2 // Would be possible with these changes</div><div><br></div><div>Thanks for your consideration -</div><div>Jake Heiser</div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=EmLgVGNgdilXys3cXWyAWvOFC2-2FejnHkMVv8PyEdfbrkRdoAm4UgDYsWeBcSbUeKgbJlXDQXd2P7x46hmVQ1vfD-2F3-2BaxIvb9W2X7v5NO4nn5DRNwNDFy6i39iAXkynVifH0QuSJur8TP-2FwMbaJ7mBu7vBFP15PDehJ-2F9PwkjEsJv0M-2F5UT2OrWrxQA19GX3XhIWB5zOo45pvGz2qgzRYj0V8RPk7IsPLekp1dvyKnhc-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important">
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div><div dir="ltr">-- <br></div>Javier Soto