1

I want to implement next code structure:

// Base hierarchy definition
protocol A {}
protocol C {
    var a: A { get set }
}

// Specific definition
protocol B: A {}    
struct TestClass: C {
    var a: B
}
  1. Protocol C requires variable of type A.
  2. Protocol B was inherited from A.
  3. Class TestClass implements C protocol by defining variable of type B (what should be ok in order of 2)

But Swift compiler complains that TestClass doesn't conform to protocol C because Candidate has non-matching type 'B'.

Why Swift compiler takes type B as non-matching to type A?

supp-f
  • 1,317
  • 1
  • 9
  • 14
  • 1
    See also https://stackoverflow.com/q/40410884/2976878 – a read-write property requirement must be invariant. The protocol `C` says you can assign an *arbitrary* `A` conforming instance to `a`, but `TestClass` says it must be a `B`. – Hamish Sep 12 '17 at 11:33
  • Yes, its exactly the answer to my question, thanks. – supp-f Sep 12 '17 at 11:42

0 Answers0