Test for protocol conformance with associated types

I am curious if it is possible to test for protocol conformance when the protocol has an associated type. I know you cannot use a protocol as a type when it has an associated type, but it seems the protocol conformance test should work.


protocol Parent {
    typealias Child
    var children: [Child] { get set }
}

class Foo: Parent {
    typealias Child = Int
    var children = [Child]()
}

let test = Foo()

if test is Parent {
    // error: protocol 'Parent' can only be used as a generic constraint because it has Self or associated type requirements
}
Accepted Answer

I'm not sure but I don't think you can.

A workaround may be to have your Parent protocol be the only one to conform to some specific (empty) "tag" protocol.

protocol ParentType {}

protocol Parent : ParentType {
    typealias Child
    var children: [Child] { get set }
}


And then you check against that (ParentType) protocol instead.

Test for protocol conformance with associated types
 
 
Q