shouldn't cast(Object)cast(void*)anything work?

Davidl Davidl at 126.com
Wed Apr 4 14:05:22 PDT 2007


for something with the same size as a pointer like , int, int*, i hope it  
can be
directly cast to cast(void*), coz nowadays d has a particularly strict  
cast system
and the casting something not Object to any Class now seems impossible at  
all without
union trick(even u can't cast void* to it) . This is really uncomfortable.  
coz extra
runtime var would be required

consider :
class Expression{}
Expression EXP_CANT_INTERPRET = cast(Expression)cast(void*)1; // this  
won't compile

you need to:

union __exp
{
       Expression _m_exp;
       int i;
}

__exp EXP_CANT_INTERPRET;

static this()
{
     EXP_CANT_INTERPRET.i=1;
}

and i don't get why i can't use the following work:

//in order to get rid of the runtime extra var

union _EXP_CANT_INTERPRET  // coz we can't use an union as an instance
{
     Expresssion _m_exp;
     int i=1;
}
_EXP_CANT_INTERPRET EXP_CANT_INTERPRET;  //compiler complains overlap  
initializer?? strange

and this won't work either:

struct _EXP_CANT_INTERPRET
{
    union
    {
        Expression _m_exp;
        int i=1;		//this would either complain overlap initializer of  
struct _EXP_CANT_INTERPRET
    }
}
_EXP_CANT_INTERPRET EXP_CANT_INTERPRET

any effort of bringing EXP_CANT_INTERPRET to compile time would be  
impossible,
that's quite painful IMO. Did i miss some better solutions?



More information about the Digitalmars-d mailing list