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
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@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