[swift-evolution] [Proposal] More Powerful Constraints for Associated Types
Dmitri Gribenko
gribozavr at gmail.com
Fri Apr 29 03:59:55 CDT 2016
On Fri, Apr 29, 2016 at 1:50 AM, David Hart <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>*/
More information about the swift-evolution
mailing list