[swift-evolution] [Re-Review] SE-0104: Protocol-oriented integers

Max Moiseev moiseev at apple.com
Thu Feb 23 12:40:02 CST 2017


Conformance to Comparable is not required by anything in the standard library. Besides, it is always possible to further constrain your own code as in:

func f<T : Number>(_ x: T) where T.Magnitude : Comparable {}

I would argue that adding constraints without solid proof of them being useful and necessary is not the right thing to do.
Also, sorting things by magnitude will require using a predicate-based sorted() anyway, and that does not require Comparable.

Max

> On Feb 23, 2017, at 9:59 AM, David Sweeris via swift-evolution <swift-evolution at swift.org> wrote:
> 
>> 
>> On Feb 23, 2017, at 9:54 AM, Karl Wagner <razielim at gmail.com <mailto:razielim at gmail.com>> wrote:
>> 
>> 
>>> On 23 Feb 2017, at 18:40, David Sweeris <davesweeris at mac.com <mailto:davesweeris at mac.com>> wrote:
>>> 
>>> 
>>> On Feb 22, 2017, at 23:52, Karl Wagner via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>> 
>>>> 
>>>>>> 
>>>>>> Are there Number types which can’t be Comparable?
>>>>>> 
>>>>>> Complex numbers. I believe `Number` is designed to allow a complex number type to conform.
>>>>>>  
>>>> 
>>>> Magnitude could be comparable then. For vectors, matrices and complex numbers, “magnitude” typically refers to a scalar. I don’t think we need something broader than that.
>>> 
>>> Using magnitude for comparison leads to some decidedly odd places:
>>> let x: Complex = 1
>>> let y: Complex = -2
>>> x < y // returns true
>>> 
>>> - Dave Sweeris 
>> 
>> 
>> Fixed:
>>> let x: Complex = 1
>>> let y: Complex = -2
>>> x.magnitude < y.magnitude // returns true
>> 
>> I don’t mean we should compare all numbers by magnitude, only that the Magnitude associated type be required to be comparable. That way you could, for example, sort things by magnitude (if that’s useful to you).
>> 
>> Given that magnitudes appear to always be scalars (I couldn’t find an exact definition beyond a “norm”, which is a scalar), it seems fair to require them to be comparable.
> 
> Ah, ok, I misunderstood you. Yes, that seems reasonable to me.
> 
> - Dave Sweeris
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170223/2531023a/attachment.html>


More information about the swift-evolution mailing list