www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - is questions

reply Lucas Goss <lgoss007 gmail.com> writes:
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

2)

1) I can't seem to get "is" to work with interfaces, what's the syntax? 
Attached is the code I want to work. I just want to be able to tell if a 
class is of a certain type of interface.

2) Does anyone else find the "is" expression a little convoluted? Is 
there any reason "is" can't be used like:

if(x is int)
   ... do some int stuff

if(x is IStream)
   ... do some stream stuff

I know it can be used that way for checking null, which I couldn't find 
the documentation for using "is" with null (it would be nice if the 
keywords in the lexical section had links to documentation on the 
keyword), but why is it used two different ways? Is there a reason it 
can't be used with types as in above? Maybe I just haven't thought this 
out enough, but the above seems much easier.
Mar 16 2006
parent reply pragma <pragma_member pathlink.com> writes:
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
Mar 16 2006
parent reply Lucas Goss <lgoss007 gmail.com> writes:
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) )
Mar 16 2006
next sibling parent reply Kramer <Kramer_member pathlink.com> writes:
Comments inline.

In article <dvci0j$1lj9$1 digitaldaemon.com>, Lucas Goss says...
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)

This is under the Identity Expressions section in http://www.digitalmars.com/d/expression.html
instead of:

if( is(x : int) )

And this is under the IsExpression section in http://www.digitalmars.com/d/expression.html This is what I'm assuming you found out. I'm glad you brought this up though as it forced me to look over that documentation again. -Kramer
Mar 16 2006
parent Lucas Goss <lgoss007 gmail.com> writes:
Kramer wrote:
 Comments inline.
 if(x is int)

This is under the Identity Expressions section in http://www.digitalmars.com/d/expression.html
 instead of:

 if( is(x : int) )

And this is under the IsExpression section in http://www.digitalmars.com/d/expression.html This is what I'm assuming you found out. I'm glad you brought this up though as it forced me to look over that documentation again.

Ah... so "is" has two different meanings... <sarcastic> great!!! </sarcastic> Oh and the "is" identity expression has two meanings itself (one for classes, static/dynamic arrays and one for everything else). <sarcastic> double great!!! </sarcastic> Thanks for the clarification.
Mar 16 2006
prev sibling parent reply John Reimer <terminal.node gmail.com> writes:
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
Mar 16 2006
next sibling parent Lucas Goss <lgoss007 gmail.com> writes:
John Reimer wrote:
 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.

I remember the === thing being changed to is, but I was more of a spectator of the language at that point in time. I didn't realize there were two different meanings to is. I can see the usefulness, it's the unusualness that sort of caught me. D definitely has some nice features, but the language can just be so odd at times. I think I shall call it Dodd :) ...hmm, so the language to "fix" D would then be Even? Or maybe Dnormal?
Mar 16 2006
prev sibling parent reply "Unknown W. Brackets" <unknown simplemachines.org> writes:
iftype.

-[Unknown]


 I think 'is()' used to be in another form, but I can't remember what it was.

Mar 16 2006
parent reply John Reimer <terminal.node gmail.com> writes:
Unknown W. Brackets wrote:
 iftype.
 
 -[Unknown]
 
 
 I think 'is()' used to be in another form, but I can't remember what 
 it was.


Ah yes, thanks. 'iftype' almost seems more clear. :P
Mar 16 2006
parent Lucas Goss <lgoss007 gmail.com> writes:
John Reimer wrote:
 Unknown W. Brackets wrote:
 iftype.

 -[Unknown]


 I think 'is()' used to be in another form, but I can't remember what 
 it was.


Ah yes, thanks. 'iftype' almost seems more clear. :P

Ah, I find it more clear as well.
Mar 17 2006