[swift-evolution] Equality of optional arrays
Jake Heiser
jakeheiser1 at gmail.com
Thu Dec 10 20:00:17 CST 2015
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151211/d74f869a/attachment.html>
More information about the swift-evolution
mailing list