D modeling

CheeseWiz CheesyFritos at gmail.com
Tue Jul 2 18:11:37 UTC 2019


On Tuesday, 2 July 2019 at 13:52:17 UTC, FeepingCreature wrote:
> On Tuesday, 2 July 2019 at 01:21:56 UTC, CheeseWiz wrote:
>> 3. Oop contravariance breaks the model logic and mixes 
>> dependencies. It is a non-issue in modeling. In D it requires 
>> us to cast everywhere. This is the main issue. You can see the 
>> issue at work using IsItTasty functions. It's simple, 
>> Contravariance prevents us staying with the derived model, we 
>> have to use the base model(for return types, same applies to 
>> arguments with covariance). But when we are in a derived model 
>> we are in it, we are using everything in the derived model 
>> that belongs to it(and behind them is the base model, but we 
>> do not directly refer to the base model), at least that would 
>> be the plan.
>>
>
> Quite the opposite! Covariance actually saves you.
>
> struct ModelB
> {	
>     interface iAnimal : ModelA.iAnimal
>     {
>         override iFood LikesWhichFood();
>     }

Ok, adding the follow does seem to work:

     interface iAnimal : ModelA.iAnimal
     {
		override iFood LikesWhichFood();
     }

then one has to do

     class Animal : ModelA.Animal, iAnimal
     {
// Note the cast, but it is ok if necessary since at least it 
would isolate it.
		 override iFood LikesWhichFood() { return 
cast(iFood)super.LikesWhichFood; }
     }


The goal here is to avoid having to add all the extra plumbing. 
Imagine a real project with 100's of classes and having to 
override and cast everything just to get it to function. It seems 
excessive. Maybe meta programming can fix it?



More information about the Digitalmars-d mailing list