[swift-users] Collection Oddities

Dave Abrahams dabrahams at apple.com
Thu Feb 9 02:32:46 CST 2017

on Tue Feb 07 2017, Guillaume Lessard <swift-users-AT-swift.org> wrote:

>> 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.

There are still plenty of 32-bit platforms that address 64-bit files,
which should be able to be modeled as Collections.


More information about the swift-users mailing list