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

Kapps opantm2+spam at gmail.com
Thu Mar 20 22:12:51 PDT 2014


On Thursday, 20 March 2014 at 21:32:08 UTC, dnspies wrote:
> 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.

If the compiler can statically determine that the cast is invalid 
you get an error instead. You can use something like 'static 
if(is(T : Foo))' to test at compile-time if T is implicitly 
convertible to Foo (I'm not certain the exact semantics) or 
'static if(is(typeof(cast(Foo)T.init)))'.

There's probably better ways to do it, and I recommend asking on 
D.learn to figure out how to do what you're looking for.



More information about the Digitalmars-d mailing list