The following code gives me an error on line return p.foo(self)
. The error says: 'P' does not have a member named 'foo'
.
protocol P {
typealias T
func foo(c: C<T>) -> T
func foo2() -> T
}
class C<T> {
var p: P
init (p: P) {
self.p = p
}
func bar() -> T {
return p.foo(self);
}
}
The protocol P
defines an associated type which should match correctly with any specialized C type. Am I missing something? Or not?
Protocol 'SomeProtocol' can only be used as a generic constraint because it has Self or associated type requirements. Code that uses a protocol that relies on associated types pays the price. Such code must be written using generic types. Generic types are also placeholders.
What is an associated type? An associated type can be seen as a replacement of a specific type within a protocol definition. In other words: it's a placeholder name of a type to use until the protocol is adopted and the exact type is specified.
Generics allow you to declare a variable which, on execution, may be assigned to a set of types defined by us. In Swift, an array can hold data of any type. If we need an array of integers, strings, or floats, we can create one with the Swift standard library.
I'll rename the types a bit before answering the question to make the problem a bit clearer:
protocol P {
typealias ElementType
func foo(c: C<ElementType>) -> ElementType
func foo2() -> ElementType
}
class C<T> {
var p: P
init (p: P) {
self.p = p
}
func bar() -> T {
return p.foo(self)
}
}
In that case you get three compiler errors:
error: <EXPR>:8:12: error: protocol 'P' can only be used as a generic constraint because it has Self or associated type requirements
var p: P
^
<EXPR>:9:14: error: protocol 'P' can only be used as a generic constraint because it has Self or associated type requirements
init (p: P) {
^
<EXPR>:13:16: error: 'P' does not have a member named 'foo'
return p.foo(self)
^ ~~~
The interesting one is the first/second one (they point out the same problem): "protocol 'P' can only be used as a generic constraint because it has Self or associated type requirements".
So the problem is the associated type. In the current configuration, you specify that the parameter for the initializer and the variable are of type P. But because you specified an associated type for P, that type is not specific enough to be used as a proper type. Only subtypes that actually specify what the ElementType
is can be used. However, you can specific a generic parameter that has to be a subtype of P. In the case of the initializer you could write
init <S:P>(p: S) {
self.p = p
}
which would eliminate the compiler error for the initializer. Now the compiler knows the parameter has to be a subtype of P and a valid subtype always specifies what the ElementType is, so it is happy.
But this doesn't help you with this line:
var p: P
You still can't use the incomplete type P
here. You would probably want to use S
, but at the moment there is no connection between the S
in the initializer and an S you would use as the type for you variable but they obviously need to be the same.
Time to introduce a second generic parameter to your class:
class C<T, S:P> {
var p: S
init (p: S) {
self.p = p
}
func bar() -> T {
return p.foo(self)
}
}
Almost done, now you have a properly specified type to use for your variable. But no your protocol specification is incorrect:
func foo(c: C<ElementType>) -> ElementType
C now takes two parameters and you need to specify these here. We would like to use `C here, but we can't:
error: :3:17: error
: type 'P' does not conform to protocol 'P'
func foo(c: C<ElementType, P>) -> ElementType
^
<EXPR>:2:15: note: associated type 'ElementType' prevents protocol from conforming to itself
typealias ElementType
Since P
does not specify the associated type ElementType
it does not properly conform to P
and can't be used in a place where a type conforming to P
is needed. But there is a nice special type: Self
. That references the actual type of the implementing protocol, so we can write the following:
protocol P {
typealias ElementType
func foo(c: C<ElementType, Self>) -> ElementType
func foo2() -> ElementType
}
Now we specified that the foo-function that is implemented by any confirming type actually takes a C with the specified ElementType and the implementing type itself. Fancy, isn't it?
But we aren't fully done yet, one last error remains:
error: <EXPR>:13:18: error: cannot convert the expression's type 'T' to type 'S.ElementType'
return p.foo(self)
At this point the compiler knows the following:
ElementType
of S
T
But there is nothing to tell it, that ElementType
and T
are actually the same, so it can't be sure whether this works and complains. So what we actually want is that the ElementType
of S
is always the same as T
and we can specify this:
class C<T, S:P where S.ElementType == T> {
Complete code:
protocol P {
typealias ElementType
func foo(c: C<ElementType, Self>) -> ElementType
func foo2() -> ElementType
}
class C<T, S:P where S.ElementType == T> {
var p: S
init (p: S) {
self.p = p
}
func bar() -> T {
return p.foo(self);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With