[swift-evolution] [Proposal] More Powerful Constraints for Associated Types

Dmitri Gribenko gribozavr at gmail.com
Fri Apr 29 10:51:11 CDT 2016


On Fri, Apr 29, 2016 at 4:02 AM, David Hart <david at hartbit.com> wrote:
> But Collection has to have an extension which explicitly mentions
> IndexingIterator:
>
> extension Collection where Iterator == IndexingIterator<Self> {
>     /// Returns an iterator over the elements of the collection.
>     public func makeIterator() -> IndexingIterator<Self> {
>         return IndexingIterator(_elements: self)
>     }
> }
>
> And Array, for example, has to be explicit:
>
> public typealias Iterator = IndexingIterator<${Self}>

This might be a workaround for a compiler bug (that it couldn't infer the type).

> So I still don’t see how defining:
>
> associatedtype Iterator : IteratorProtocol = IndexingIterator<Self>
>
> has created any default behaviour.
>
> When you say "The fact that the compiler does not allow to use the inferred
> ‘Foo' type actually looks like a bug to me.”, could you give me an example
> of how you would have imagined this default behaviour to work in the case of
> Array? Would have expected Array not to have to define its typealias?

Right, I would expect Array to not need to mention Iterator at all in
its definition.

Take a look at stdlib/public/core/CollectionOfOne.swift.  It does not
define SubSequence or Indices.

Dmitri

-- 
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/


More information about the swift-evolution mailing list