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
}
I just don’t see the implicit use of the default associated type.
David.
> On 29 Apr 2016, at 09:36, Dmitri Gribenko <[email protected]> wrote:
>
> On Fri, Apr 29, 2016 at 12:25 AM, David Hart via swift-evolution
> <[email protected]> wrote:
>> I’ve taken some time to digest the current feedback and I’ve changed my
>> mind. The syntax for adding constraints to a sub-protocol in the protocol’s
>> definition where clause is starting to grow on me. Before I modify the
>> proposal, I'd still like to understand something:
>>
>> What is the use of declaring a default associated types with the `=` syntax
>> in protocols? I’ve never used them and I don’t understand what they provide.
>
> Please take a look at the Collection protocol
> (https://github.com/apple/swift/blob/master/stdlib/public/core/Collection.swift).
>
> associatedtype Iterator : IteratorProtocol = IndexingIterator<Self>
> associatedtype SubSequence : IndexableBase, Sequence = Slice<Self>
> associatedtype Indices : IndexableBase, Sequence = DefaultIndices<Self>
>
> Some collections want to customize these, but for simple ones the
> defaults are usually fine.
>
> 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 <[email protected]>*/
_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution