Post not yet marked as solved
Post marked as unsolved with 3 replies, 648 views
protocol P : AnyObject {}
class C<T: AnyObject> {}
let q = C<P>()
results in:
expression failed to parse:
error: AnyObjectProtocol.playground:5:9: error: 'C' requires that 'any P' be a class type
let q = C<P>()
^
AnyObjectProtocol.playground:4:7: note: requirement specified as 'T' : 'AnyObject' [with T = P]
class C<T: AnyObject> {}
^
This is really confusing since the following code seems to recognise that values of type P can be cast to AnyObject:
protocol P : AnyObject {}
class A: P{}
class B: P{}
let h: (AnyObject) -> () = { print($0) }
let f: (P) -> () = { h($0) }
f(A())
f(B())
And it's not like protocols couldn't be used as type parameters for generic classes
protocol P : AnyObject {}
class D: P {}
class G1<T> {}
class G2<T: AnyObject>{}
let k = G1<P>()
let m = G1<D>()
let n = G2<D>()
let z = G2<P>() // 'G2' requires that 'any P' be a class type
^ only the last line (with z) causes compilation error
Is it a compiler bug?