Method override with a generic signature with requirements not imposed by the base method

Arrays are covariant, which is incorrect for the same reasons, but being value types, and thanks I guess to some runtime juggling, they actually work, and allow for something like the following:

protocol A {}
protocol B: A {}
protocol C: A {}

extension String: B {}

var listOfB: [B] = ["howdy"]

var listOfA = listOfB as [A]

listOfA is [B] /// == `true`
listOfA as! [B] /// ok

extension Int: C {}

listOfA.append(42)

listOfA is [B] /// == `false`
listOfA as! [B] /// crash

Of course this is not going to be a problem in the 99,99% of times, but if one weirdly thinks to downcast a previously upcasted array, they could be in for a treat :smiley:

1 Like