General problem I'm having in D with the type system

Vijay Nayar madric at gmail.com
Sun May 27 06:59:43 UTC 2018


On Sunday, 27 May 2018 at 06:00:30 UTC, IntegratedDimensions 
wrote:


The problem description is not very clear, but the catfood 
example gives a bit more to work with.

> animal  ->   food
>   |            |
>   v            v
> cat     ->   catfood
>
>
> Of course, I'm not sure how to avoid the problem in D of
>
>
> animal a = new cat();
>
> a.f = new food()
> auto c = cast(cat)a;

Cast operations are generally not guaranteed to preserve type 
safety and should be avoided when possible.  But if I understand 
your description, you have the following relations and 
transitions:

   animal owns food
   cat    owns catfood
   animal may be treated as a cat (hence the casting)
   food may be treated as a catfood (hence the casting)

It may be that the inheritance relationship is backwards in your 
use case.  If "animal" may be treated as a "cat", then the 
inheritance should be other other way around, and "animal" would 
inherit from "cat".

What specific kinds of relationships are you trying to model 
among what kinds of entities?



More information about the Digitalmars-d mailing list