Checking constraints on associated types within a protocol

There is an error in your reasoning.

As you say, String does not conform to VectorArithmetic. Therefore, your property var animatableData: String does not fulfill the protocol requirement.

You are correct that default implementations are overridden when you provide your own implementation of the requirement: but here you did not provide an implementation that fulfills the requirement, so the default implementation is not overridden.

In a context where the the value ma is of concrete type MyAnimatable, your property shadows but does not override the required property, which is very much still there and fulfilled by the default implementation. You can prove this to yourself by passing your value to a generic function:

let ma = MyAnimatable()
print(type(of: ma.animatableData)) // This will print `String`

func f<T: Animatable>(_ t: T) {
  print(type(of: t.animatableData))
}
f(ma) // This will print `EmptyAnimatableData`
4 Likes