[swift-evolution] protocol-oriented integers (take 2)

Karl Wagner razielim at gmail.com
Mon Jan 16 15:32:02 CST 2017


> On 16 Jan 2017, at 20:53, Xiaodi Wu <xiaodi.wu at gmail.com> wrote:
> 
> On Mon, Jan 16, 2017 at 11:57 AM, Karl Wagner via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> Really glad to see this coming back :)
> 
> I have a couple of questions:
> 
> 1) How does Strideable relate to Arithmetic?
> 
> My understanding is that Strideable allows non-numeric types to express different-type addition and subtraction, and that Arithmetic is for numeric types only (derives from ExpressibleByIntegerLiteral) and allows then to express same-type operations. Is that correct?
> 
> My understanding was that heterogeneous addition and subtraction using operators are not supported not merely due to compiler limitations but as a fundamental philosophy, because the type of the intended result can be unclear to human readers and unintentional promotion is to be avoided? In that vein, my understanding was that having those operators available for Strideable was inconsistent and overdue for removal, and `advanced(by:)` is the intended spelling for advancing a value by a distance of a different type.
>  
> If so, it would be nice to add it to the proposal so people know when they should conform (or set their generic constraints) to one or the other.
> 
> 2) Can SignedNumber get some operators now?
> 
> Strideable.Stride is bound to this weird protocol ‘SignedNumber’. You can create instances of it out of thin air via an integer literal, but you can’t accumulate Strides to find the overall distance between some adjacent Strideables.
> 
> I understood these new protocols to mean that SignedNumber is headed for deprecation?

It would be nice if the proposal said that, then (and mentioned the corresponding change to Strideable.Stride). I assume it will be replaced with SignedArithmetic, then.

>  
> Also, #1 is why I don’t like the Strideable operator deprecation being part of the proposal (and therefore part of the language specification). It doesn’t solve the underlying issue, which is that our integer literal protocols aren’t really good enough - they’re not useful enough outside the standard library (see: https://bugs.swift.org/browse/SR-920 <https://bugs.swift.org/browse/SR-920>), and evidently the compiler isn’t able to use them to infer types correctly. Perhaps fixing it is a low priority, and that’s fine, but what about if somebody else defines this operator in their library? They start of break lots of valid code because the compiler can’t do the right thing. Improved literal protocols may be the better way to resolve that problem.
> 
> I agree totally that literals are aching for improvements as outlined in SR-920. Not sure how it relates to integer protocols though. What are some examples of valid code that get broken today because of compiler limitations?

The PR that implemented this fixed a bug with integer literals - basically, the thrust of it (as I understand) is that the compiler can’t figure out which is the preferred overload. It doesn’t know to prefer same-type arithmetic when resolving literals involved in operators.

My problem with just fixing the problem by removing that operator is that anybody could add it back in their own code, and that should be perfectly allowable, but if they did so basic expressions would fail to compile. To me it sounds preferable to leave this as a implementation artefact until we get a chance to make the literal protocols better.

Strideable isn’t some esoteric protocol just for collection indexes, you know. It’s very useful for any quasi-numeric type whose absolute value is semantically different from a difference. For example, I have strongly-typed wrappers for a ByteOffset within a binary file and a LineNumber inside a text file. These wrappers allow me to write extensions (for example to clamp to the bounds of a particular file), and ensure that I don’t accidentally mix semantically different values. They are both Strideable by Int, which is a better semantic fit than Arithmetic (LineNumbers are absolute, should you be able to add them? What does LineNumber * LineNumber mean?).

So my point is basically that it’s an important protocol (maybe not so much in the standard library, but certainly beyond).

- Karl

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170116/ad7cfd13/attachment.html>


More information about the swift-evolution mailing list