[swift-evolution] [Draft] Rename Sequence.elementsEqual

Ole Begemann ole at oleb.net
Wed Oct 18 07:28:19 CDT 2017


On Tue, Oct 17, 2017, at 20:46, Xiaodi Wu via swift-evolution wrote:
> On Tue, Oct 17, 2017 at 12:54 Jonathan Hull <jhull at gbis.com> wrote:
> 
>> Why was elementsEqual created?  Isn’t it meant to check equality of
>> two sequences in a generic context where == isn’t available?> 
> No no no no no no no no. That’s precisely why the name is misleading.
> elementsEqual is *not* simply a mixed-type version of ==. Remember
> that == as implemented on a concrete sequence type *has no obligation*
> to use elementwise comparison using the element’s implementation of
> ==. This is not merely theoretical: [Float].== does *not* do an
> elementwise comparison using Float.==. By contrast, you are guaranteed
> a true elementwise comparison with elementsEqual regardless of how
> equivalence is defined for the sequence.
Really? I would have thought an elementwise comparison is exactly what
[Float].== would do.
I tested this and these are the (very surprising, at least to me)
results:
let numbers: [Float] = [1, .nan]
let copy = numbers
let sameNumbers: [Float] = [1, .nan]

numbers == copy                     // (1) true (!)
numbers == sameNumbers              // (2) false (!)
numbers.elementsEqual(copy)         // (3) false (ok)
numbers.elementsEqual(sameNumbers)  // (4) false (ok)

So [Float].== returns a different result depending on whether you
compare two arrays that share the same buffer (1) or two arrays with the
same contents (2) when the array contains NaN. The reason is that
Array.== immediately returns true if two arrays share the same
buffer[1].
Even knowing that comparing floats is problematic in the context of NaN
(and in general), I find this very surprising. It also seems to clash
with Michael's idea[2] that two substitutable sequences should return
true for ==.

Links:

  1. https://github.com/apple/swift/blob/master/stdlib/public/core/Arrays.swift.gyb#L2194-L2197
  2. https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20171016/040544.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171018/4fc174ef/attachment.html>


More information about the swift-evolution mailing list