[swift-dev] Rationalizing FloatingPoint conformance to Equatable

Xiaodi Wu xiaodi.wu at gmail.com
Tue Nov 14 18:44:35 CST 2017


On Tue, Nov 14, 2017 at 2:34 PM, David Sweeris <davesweeris at mac.com> wrote:

>
> On Nov 2, 2017, at 4:22 PM, Xiaodi Wu via swift-dev <swift-dev at swift.org>
> wrote:
>
> On Thu, Nov 2, 2017 at 5:22 PM, Matthew Johnson <matthew at anandabits.com>
> wrote:
>
>>
>> On Nov 2, 2017, at 5:20 PM, Jonathan Hull via swift-dev <
>> swift-dev at swift.org> wrote:
>>
>> It looks like we have a good solution.  Per Steve and David’s suggestions:
>>
>> 1) Make FloatingPoint == reflexive
>>
>> 2) Add &== to FloatingPoint for those who specifically want IEEE behavior
>>
>> 3) Add a warning + fixit to ‘a != a’
>>
>> We should take this to evolution…
>>
>>
>> Looks like a winner to me.
>>
>
> Again, there remain several problems with this design. In the concrete
> context, the syntax `&==` suggests that it is a compatibility, legacy, or
> specialized function not to be preferred over `==`. This makes Swift
> deviate from every other programming language, creating a new footgun for
> experienced developers, and encourages a performance hit where one is not
> demonstrably necessary (most operations that ask about UI coordinates, say,
> will never have NaN as an input). Again also, this design eliminates the
> possibility of writing a class of useful algorithms that use Numeric. It
> also doesn't address the problem of sorting (as NaN would still compare
> unordered to all other values). These are serious questions that require
> careful consideration.
>
> In initiating this conversation, my goal has been to start a conversation
> about possible designs for `Equatable` and `Comparable` that permis useful
> generic algorithms to work correctly with floating point without hampering
> performance or changing syntax for concrete numerics. Unless this is
> clearly impossible, the leap of changing the behavior of `==` for a numeric
> type should absolutely not be the proposed solution, in my view.
>
>
> What about bringing `===` into the mix? IIRC `identity`semantics aren't
> well-defined for value types anyway. Is there even an "Identifiable"
> protocol in the stdlib?
>

That is, indeed, one avenue that can be explored. (See Swift Evolution
archives for more on that.) But, it is actually possible to get very close
to squaring the circle here, and when I have some time I will share some
drafts with the list.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-dev/attachments/20171114/00ebd9f3/attachment.html>


More information about the swift-dev mailing list