Adding a new design constraint to D

The Zealot zod at zod.zod
Tue Jun 14 12:58:09 UTC 2022


>>> import b;
>>> void handle(Bar child) if(isDerivedFrom!(Foo, Bar))
>>> {
>>>   (cast(Foo)child)._c += child.c;
>>> }
>>
>> No it does not. You should never have to cast to a parent 
>> type, NEVER!
>>
>> That is a red flag if you have to do that, a major one at that.
>
> no this is actually perfectly fine, as the code ensures at 
> compiletime that Bar is in fact a subtype of Foo, so the cast 
> is always valid. You have to be explicit that you actually want 
> to use Foo's _c; or else adding a _c variable to Bar silently 
> changes the behaviour of your code. This works as intended.

and just to be clear, you don't even need to do it with an 
explicit cast.
All you have to do is access _c through a baseclass pointer.


More information about the Digitalmars-d mailing list