[swift-evolution] [Proposal] More Powerful Constraints for Associated Types
David Hart
david at hartbit.com
Fri Apr 29 06:02:37 CDT 2016
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}>
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?
David.
> On 29 Apr 2016, at 10:59, Dmitri Gribenko <gribozavr at gmail.com> wrote:
>
> On Fri, Apr 29, 2016 at 1:50 AM, David Hart <david at hartbit.com <mailto:david at hartbit.com>> wrote:
>> Hi Dimitri,
>>
>> Excuse me if I’m being dumb. I saw those example but I don’t understand
>> their use.
>>
>> If I define a custom Collection, I still have the be explicit in the types I
>> use where associated types are expected. Because I need to be explicit, I
>> don’t understand how default associated types are used.
>>
>> Default parameters seem clear to me:
>>
>> func foobar(a: Int = 0) {}
>> foo() // implicit uses default value
>> foo(a: 1) // Explicit bypasses default value
>>
>> But default associated types are not:
>>
>> protocol Foobar {
>> associatedtype Foo = Int
>> func foobar(a: Foo)
>> }
>>
>> class FoobarImpl : FooBar {
>> func foobar(a: Foo) {} // error: Use of undeclared type ‘Foo’ - there is
>> no such thing as an implicit use
>> func foobar(a: Double) {} // This works but is explicit
>> }
>
> In a typical use, 'func foobar' would also be defaulted. The
> Collection.Iterator associated type and the makeIterator() method are
> an example of this.
>
> The fact that the compiler does not allow to use the inferred 'Foo'
> type actually looks like a bug to me.
>
> 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 <mailto:gribozavr at gmail.com>>*/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160429/1a2d5528/attachment.html>
More information about the swift-evolution
mailing list