strettosis at tutanota.com

Stretto via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jun 9 15:19:33 PDT 2016


I have some class like

class bar { }
class foo : bar
{
     bar[] stuff;
}

and have another class

class dong : bar
{
    int x;
}


Now sometimes stuff will contain dong's, but I cannot access its 
members it without a cast.

fooo.stuff[0].x // invalid because bar doesn't contain x;

Hence,

((cast(dong)foo.stuff[0]).x is the normal way with a possible 
type check.

But in my case I will never mix different types in stuff and will 
always use it properly or do type checking in the cases I might 
mix.

Rather than add a dong[] dongs; to foo, which increases the size 
of foo and wastes memory just to prevent the cast, I'm curious if 
there is any other way to solve this problem?

I simply want to do foo.stuff[0].x and have foo.stuff[0] be 
treated as an image.

Is an opDispatch and/or opIndex required or is there some alias 
trick that can be used?

I'd rather access like foo.dongs[0].x without defining a dong 
array directly in foo, but simply alias to stuff with an implicit 
cast to dong.













More information about the Digitalmars-d-learn mailing list