is questions

John Reimer terminal.node at gmail.com
Thu Mar 16 13:41:05 PST 2006


Lucas Goss wrote:
> pragma wrote:
>> Obi-Wan: This is not the idiom you're looking for.  :)
> 
> Hahaha...
> 
>> D uses the cast() operator to accomplish runtime type checking.  The 'is'
>> operator is something else entirely (value/reference equality).
> 
> Doh... well thanks! Don't know how I missed that, I was even looking at 
> cast too. I must be getting my languages mixed up. In addition, maybe 
> the documentation is also confusing. I looked at "is" and it says:
> 
> IsExpressions are evaluated at compile time and are used for checking 
> for valid types, comparing types for equivalence, determining if one 
> type can be implicitly converted to another, and deducing the subtypes 
> of a type.
> 
> I guess I always think of a class as a custom type. Is that not the 
> correct way to think of it? Of course I still prefer this syntax:
> 
> if(x is int)
> 
> instead of:
> 
> if( is(x : int) )

if (x is null) { ... }

and

if ( is(...) ) { ... }

are used for two entirely different purposes.

The first is for testing the validity of object references and pointers 
at /run time/.

The second is for determining the validity of types at /compile time/.

It's a little unusual, but both are useful.  The first is to fix 
equality tests on objects (you can't use '==' test on objects; you must 
use 'is', which used to be '===').  The second adds more compile time 
tricks and goes along nicely with 'static if' and 'template'.  I did 
find 'is()' to be a little confusing when I first discovered it.  I 
think 'is()' used to be in another form, but I can't remember what it was.

-JJR




More information about the Digitalmars-d mailing list