The private box will be a type that implements the abstract base class. The goal of the private box is to provide a wrapper around the concrete object implementing the PAT and forward all of the calls to the boxed object.
Following the conventions also suggests that you should use _Any#MY_PROTOCOL#Box as a class name for this component:
private final class _AnyAnimalBox<A: Animal>: _AnyAnimalBase<A.FoodType> {
// The target object, that is an Animal
var target: A
init(_ target: A) {
self.target = target
}
// Overrides of the abstract classes's implementation
// Forward all invocations to the concrete target
override var name: String {
return target.name
}
override var preferredFood: A.FoodType? {
get { return target.preferredFood }
set { target.preferredFood = newValue }
}
override func eat(food: A.FoodType) {
target.eat(food: food)
}
}
As the _AnyAnimalBox class extends _AnyAnimalBase, we will be able to keep references of our boxes as their abstract class type, which successfully bridges the world of protocols and their associated types to the world of generics.