0
protocol Animal {}
struct Lion: Animal {}
protocol Zoo {
    var animals: [Animal] { get }
}
struct BronxZoo: Zoo { // error: Type 'BronxZoo' does not conform to protocol 'Zoo'
    var animals: [Lion] = []
}

If BronxZoo has an animals array of type Lion, which conforms to Animal, then why isn't BronxZoo considered to be in conformance with Zoo?

matt
  • 515,959
  • 87
  • 875
  • 1,141
slider
  • 2,736
  • 4
  • 33
  • 69
  • This has _nothing_ to do with "subprotocol properties". – matt Apr 30 '19 at 18:05
  • This is essentially a duplicate of [Why can't a get-only property requirement in a protocol be satisfied by a property which conforms?](https://stackoverflow.com/q/42561685/2976878), just with arrays – it's something that Swift should arguably be able to support, but it currently doesn't. You can use the same workarounds given in the linked Q&A, either define a 'dummy' forwarding property, or introduce an associated type. – Hamish Apr 30 '19 at 18:17
  • No, it's not a duplicate. This question assumes that Swift arrays are covariant. The answer should correct that (invalid) assumption, and the question you linked to doesn't do so. – rob mayoff Apr 30 '19 at 18:19
  • 1
    @robmayoff Swift arrays are covariant – a `[Lion]` can be converted to an `[Animal]`. – Hamish Apr 30 '19 at 18:20
  • @Hamish If you're going to edit the question you should eliminate OutdoorZoo; it is a red herring. – matt Apr 30 '19 at 18:21
  • I stand corrected. – rob mayoff Apr 30 '19 at 18:22

0 Answers0