[swift-dev] SR-122 / CollectionsMoveIndices.swift Prototype

Shawn Erickson shawnce at gmail.com
Wed Mar 9 17:52:06 CST 2016

I see a handful of issues like the following coming about because of
changes to `Range`. In the past `Range` conformed to `Collection` so
`count` came from `Collection` which returned a type of
`ForwardIndex.Distance` (e.g. `_SignedInteger`). It now comes from
`Strideable`’s `distance(to:)` which returns `Strideable.Stride` (e.g.

error: value of type 'C.Index.Stride' has no member 'toIntMax'
  let len = range.count.toIntMax()
            ~~~~~~^~~~~ ~~~~~~~~

Additionally when trying to provide `Strideable` optimized `Collection`
default implementations I hit issues like the following (note
`Collection.IndexDistance` minimally conforms to SignedInteger). I picked
`Strideable` as the optimization point since `RandomAccessCollection.Index`
minimally conforms to `Strideable` and `Strideable` provides the first
level of possible optimizations after `Comparable`.

extension Collection where Index : Strideable {
  public func next(i: Index) -> Index {
    return i.advanced(by: 1) <— this is ok
  public func advance(i: Index, by n: IndexDistance) -> Index {
    return i.advanced(by: n) <— this errors

error: cannot invoke 'advanced' with an argument list of type '(by:
    return i.advanced(by: n)
note: expected an argument list of type '(by: Self.Index.Stride)'
    return i.advanced(by: n)

Looking at things (see below ASCII art) I see that `_SignedInteger`
conforms to `SignedNumber `(and `SignedInteger` conforms to
`_SignedInteger`). `SignedNumber` doesn’t provide toIntMax nor can it act
as an `SignedInteger`. The use of `SignedNumber` in `Strideable` is hence
problematic. I guess the reason for `SignedNumber` is to let distances in
Strideable support non-integer types?

I am not sure the best path forward at the moment. Do I attempt to scope
things to `Strideable` where `Stride : SignedInteger` since
`Collection.IndexDistance` – as currently defined – doesn’t allow for
non-integer types? I am not sure the allowable scoping for Range in that
situation? ...or do we change `Strideable.Stride` to minimally conform to
`SignedInteger`? ...or?

Pardon my naïvety in this area of things,

Strideable (associatedtype Stride : SignedNumber) ->
advanced(by:Self.Stride) & distance(to: Self)
  ^              SignedNumber
  ^                 ^
Integer & _SignedInteger —> func toIntMax()
SignedInteger —> func toIntMax()
Indexable (associatedtype Index : Comparable)
Collection (associatedtype IndexDistance : SignedInteger)
RandomAccessCollection (associatedtype Index : Strideable)
MutableIndexable (associatedtype Index : Comparable)
  ^    Collection
  ^       ^
MutableCollection (associatedtype IndexDistance : SignedInteger)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-dev/attachments/20160309/b163cd22/attachment.html>

More information about the swift-dev mailing list