3

It's an isolated example, so may look less useful, but I was wondering anyway, why it doesn't work? Any insight much appreciated.

protocol Prot: class {
    init()
}

class A: Prot {
    required init(){ }
}

struct Client<T: Prot> {
    let tau: T.Type
}

if let aTau = A.self as? Prot.Type {
    print(aTau === A.self)  // ✅
    Client(tau: A.self)     // ✅
    Client(tau: aTau)       // ❌
}

The error is:

Cannot invoke initializer for type 'Client<_>' with an argument list of type '(tau: Prot.Type)'
maniacus
  • 381
  • 1
  • 3
  • 9
  • 2
    This is a variant of [Protocol doesn't conform to itself?](http://stackoverflow.com/questions/33112559/protocol-doesnt-conform-to-itself) – you cannot use `Prot` as a type that conforms to `Prot`. – Hamish Dec 09 '16 at 10:36

1 Answers1

2

The generic Client class needs a concrete type for specialization - i.e. a class/struct/enum, and Prot.Type doesn't fit this requirement. This is why you get the error.

Cristik
  • 30,989
  • 25
  • 91
  • 127