[swift-users] Collection Oddities

Guillaume Lessard glessard at tffenterprises.com
Wed Feb 8 00:03:19 CST 2017

> On 7 févr. 2017, at 21:57, Slava Pestov <spestov at apple.com> wrote:
>> On Feb 7, 2017, at 8:14 PM, Guillaume Lessard via swift-users <swift-users at swift.org> wrote:
>> I keep running into weird things with Swift 3 Collections.
>> A) Collection has its count property defined as an Int via IndexDistance:
>> public protocol Collection : Indexable, Sequence {
>>   associatedtype IndexDistance : SignedInteger = Int  // line 182 in Collection.swift
>>   public var count: IndexDistance { get }             // line 776
>> }
> This declaration specifies that the *default* associated type is Int, not that it’s *always* Int. A Collection implementation is free to use a different type as its IndexDistance if it wants.

I see how I’d misunderstood that line.

This being said, does this particular freedom really bring anything to the table? It is simply a counter (“the number of steps between a pair of indices”). On one hand, a “collection" that has in excess of Int.max/2 elements likely needs a different protocol; on the other, using a shorter type for a counter seems retro.

Guillaume Lessard

More information about the swift-users mailing list