2

I have a little problem, here is the faulty code:

protocol ConstraintProtocol { }

protocol AssocProtocol {
    associatedtype T : ConstraintProtocol
}

protocol MyProtocol : ConstraintProtocol { }

enum ConcreteType: AssocProtocol {
    typealias T = MyProtocol
    case a
}

The error states the usual type 'ConcreteType' does not conform to protocol 'AssocProtocol' even giving a hint: possibly intended match 'ConcreteType.T' (aka 'MyProtocol') does not conform to 'ConstraintProtocol'

But... MyProtocol does conform to ConstraintProtocol!!!

And indeed, if I remove the constraint from the associatedtype, it works like a charm.

So, is it just me, or did I miss something?

I'm using Swift 4.1 in Xcode 9.3.1, if it can help.

Zaphod
  • 6,758
  • 3
  • 40
  • 60
  • 7
    [Protocols don't always conform to themselves](https://stackoverflow.com/a/43408193/2976878) – although `MyProtocol` derives from `ConstraintProtocol`, it does not currently conform to `ConstraintProtocol`, therefore it cannot be used to satisfy the associated type `T : ConstraintProtocol`. – Hamish May 31 '18 at 15:34
  • Damnit! Thanks for the link. I did not know that Swift specificity... – Zaphod May 31 '18 at 17:12

0 Answers0