[swift-evolution] Equality of optional arrays

Javier Soto javier.api at gmail.com
Thu Dec 10 20:03:54 CST 2015


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 :)
On Thu, Dec 10, 2015 at 6:00 PM Jake Heiser via swift-evolution <
swift-evolution at swift.org> wrote:

> Hi all!
>
> Currently in Swift, checking the equality of two optional arrays is much
> messier than it should be:
>
> switch (optionalArray1, optionalArray2) {
>     case (.Some(let a1), .Some(let a2)) where a1 == a2: return true
>     case (.None, .None): return true
>     default: return false
> }
>
> 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):
>
> public func ==<T : Equatable>(lhs: T?, rhs: T?) -> Bool
> public func ==<Element : Equatable>(lhs: ArraySlice<Element>, rhs:
> ArraySlice<Element>) -> Bool
> public func ==<Element : Equatable>(lhs: [Element], rhs: [Element]) -> Bool
>
> To correct the issue, I propose two functions be added to the standard
> library:
>
> public func ==<Element : Equatable>(lhs: ArraySlice<Element>?, rhs:
> ArraySlice<Element>?) -> Bool
> public func ==<Element : Equatable>(lhs: [Element]?, rhs: [Element]?) ->
> Bool
>
> This change would allow optional arrays to be checked for equality just as
> other objects are:
>
> optionalString1 == optionalString2 // Already possible
> requiredArray1 == requiredArray2 // Already possible
> optionalArray1 == optionalArray2 // Would be possible with these changes
>
> Thanks for your consideration -
> Jake Heiser
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
-- 
Javier Soto
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151211/61d8d9d8/attachment.html>


More information about the swift-evolution mailing list