Cannot cast X to Y at compile time...?

dnspies dspies at ualberta.ca
Wed Mar 19 09:57:37 PDT 2014


I have a function called at CTFE which includes the lines:

97	if(conjunction exp = cast(conjunction)this_exp) {
98		inner_substitutions!(first,conjunction)(exp, map);
99	} else if(disjunction exp = cast(disjunction)this_exp) {
100		inner_substitutions!(first,disjunction)(exp, map);
101	}

Here, this_exp is a reference of type "expression" to an object 
whose CTFE-runtime-type is of type "disjunction".  conjunction 
and disjunction are both descendent classes of expression.
This code produces the following compilation error:

source/cfgparse.d(97): Error: cannot cast [...] to 
cfgparse.conjunction at compile time.

([...] stands in for a very long string which I think is some 
sort of representation of this_exp)

Just for the hell of it, I tried moving the assignment out of the 
conditional, and something very strange happens.

97	if(cast(conjunction)this_exp) {
98		conjunction exp = cast(conjunction)this_exp;
99		inner_substitutions!(first,conjunction)(exp, map);
100	} else if(cast(disjunction)this_exp) {
101		disjunction exp = cast(disjunction)this_exp;
102		inner_substitutions!(first,disjunction)(exp, map);
103	}

source/cfgparse.d(101): Error: cannot cast [...] to 
cfgparse.disjunction at compile time

Both the conditions compile properly, and now only the assignment 
fails.  Why  is this happening and how can I avoid it?



More information about the Digitalmars-d mailing list