[swift-evolution] [Draft] Rationalizing Sequence end-operation names

Xiaodi Wu xiaodi.wu at gmail.com
Tue Jun 28 13:40:33 CDT 2016


On Tue, Jun 28, 2016 at 1:25 PM, David Hart <david at hartbit.com> wrote:

>
>
> > On 28 Jun 2016, at 13:46, Brent Royal-Gordon <brent at architechies.com>
> wrote:
> >
> > Unlike the other methods, adding additional elements changed the length
> of `suffix(from:)`'s return value. That indicates to me that it is *not* a
> suffix operation at all.
>
> I don't agree with your reasoning here. It *is* an operation that returns
> the suffix (the end of the sequence) but starting from an index. The fact
> that indices index from the start of the array and that the function will
> return an array of a different length depending on the argument does not
> make the result of the operation less of a suffix. It is still returning
> the end of the array.
>

I was in the midst of writing a reply along the same lines, so I figured
I'd add to David's reply here. There are two characteristics I would expect
from a method named "prefix" or "suffix".

First, it should return a subsequence containing zero to count elements.
(By contrast, something named "first" should return nil or one element, but
certainly no more.)

Second, in the case of "prefix", the first element of the subsequence (if
any) should be the first element of the sequence; in the case of "suffix",
the last element of the subsequence (if any) should be the last element of
the sequence.

Now, `suffix(from:)` fulfills both of those expectations. Like David, I do
not understand how you arrive at the interpretation that a "suffix
operation" should be one that returns a fixed number of elements.

Replacing the word "suffix" with square brackets is inferior, IMO, because
at the call site the reader cannot immediately tell that the two
characteristics I named above would hold. First, depending on what is
inside the square brackets, a subscript could return a subsequence or it
could return a single element. Second, depending on what is inside the
square brackets, a subsequence could start or end anywhere. By contrast,
the words "prefix" and "suffix" clearly communicate what sort of
subsequence you'll get back without inspection of the argument.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160628/33bacf45/attachment.html>


More information about the swift-evolution mailing list