0
protocol A {}
protocol B: A {}
protocol C { 
    var X: A { get } 
}
struct D: C { 
    let X: B // ERROR: does not conform to protocol "C"
} 

Shouldn't this be okay since B conforms with A?

I tried to remedy it by using an associatedtype in protocol C:

protocol A {}
protocol B: A {}
protocol C {
    associatedtype SomeType: A
    var X: SomeType { get }
}
struct D: C {
    let X: B // ERROR: does not conform to protocol C -- seemingly requiring a concrete type
}
Matt
  • 463
  • 4
  • 14

1 Answers1

-1

The error makes sense if you think about it the following way.

/// It does not have any requirements
protocol A {}

/// It has to satisfy all of the requirements of A (whatever they are)
/// It does not have any of it's own requirements (currently)
/// It is expected to add some additional requirements of it's own
/// Otherwise it defeats the purpose of having this
protocol B: A {}

/// This has only one requirement, X that conforms to A
protocol C { 
    var X: A { get } 
}

/// This has only one requirement, X that conforms to B
/// The problem here is 
/// - A & B are different and can not be used interchangeably 
/// (at least in the current versions)
struct D: C { 
    let X: B // ERROR: does not conform to protocol "C"
} 

Logically it makes sense - that B has to satisfy all requirements of A and hence it should be allowed.

I think Swift Forums might be a better place for discussing this.

Tarun Tyagi
  • 9,364
  • 2
  • 17
  • 30