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

dnspies dspies at ualberta.ca
Thu Mar 20 14:32:07 PDT 2014


On Wednesday, 19 March 2014 at 20:43:59 UTC, Frustrated wrote:
> On Wednesday, 19 March 2014 at 16:57:38 UTC, dnspies wrote:
>> 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?
>
> I ran up to a similar situation when the thing trying to be 
> cast was not what I thought it was. I.e., the error is exactly 
> what it means. Try to create a this_exp that is a conjunction 
> explicitly to see if that is the problem.

Sorry, I don't understand.  When I cast something to the wrong
type, I should just get a null reference, shouldn't I?  It
shouldn't throw an error.


More information about the Digitalmars-d mailing list