On Sunday, 27 May 2018 at 18:16:25 UTC, JN wrote:
On Sunday, 27 May 2018 at 06:00:30 UTC, IntegratedDimensions wrote:
animal a = new cat();

a.f = new food()
auto c = cast(cat)a;


as now f in cat will be food rather than catfood.

I think the problem is in your hierarchy. If Animal can have Food, that means that any animal should be able to accept any food, without knowing what kind of food is this. Cat requiring cat food is a leaky abstraction, the cat shouldn't know nor care what kind of food it gets, as it's an animal and it will eat any food.

This is clearly false.

A Kola does not eat any type of food, nor does a whale or most animals.

While it is just an example, it still applies in general. Natural transformations are fundamental to type theory.


The only problem where it can leak is when we treat an cat as an animal then put in dog food in to the animal, which is valid when cat as treated as an animal, then cast back to cat. Now cat has dog food, which is invalid.

But I've already pointed out two things about this: 1. The hierarchy is maintained to be non-leaky at runtime(I never down cast). 2. casting can be designed to handle this behavior. When we cast animal to cat then the cast can null dog food since it isn't cat food. No different than if we cast animal to hammer but requires a bit more compiler logic.






Reply via email to