is questions

pragma pragma_member at pathlink.com
Thu Mar 16 11:27:39 PST 2006


Obi-Wan: This is not the idiom you're looking for.  :)

D uses the cast() operator to accomplish runtime type checking.  The 'is'
operator is something else entirely (value/reference equality).  Try this:

>int main(char[][] args)
>{
>	Tester t = new Tester();
>	
>	// Should print (is an IA type)
>	if(cast(IA)t !is null)
>	{
>		writefln("type IA");
>	}
>	
>	// Should not print (not an IB type)
>	if(cast(IB)t !is null)
>	{
>		writefln("type IB");
>	}
>	
>	return 0;
>}

The cast() operator returns null if the cast cannot be completed.  This is in
contrast to Java, which throws a CastException in such cases.  The caveat to
this behavior is that if any cast can be expected to fail, you have to check the
result for null.

> auto bar = cast(Foobar)foo;
> assert(bar !is null);

- EricAnderton at yahoo



More information about the Digitalmars-d mailing list