Jump to page: 1 24  
Page
Thread overview
Java instanceof?
Jan 23, 2004
John Reimer
Jan 23, 2004
John Reimer
Jan 23, 2004
Vathix
Jan 23, 2004
John Reimer
Jan 23, 2004
Brad Anderson
Jan 23, 2004
Vathix
Jan 23, 2004
J Anderson
Jan 23, 2004
John Reimer
Jan 23, 2004
Matthew
Jan 23, 2004
John Reimer
Jan 23, 2004
J Anderson
Jan 23, 2004
John Reimer
Jan 23, 2004
Matthew
Jan 23, 2004
Sjoerd van Leent
Jan 23, 2004
John Reimer
Jan 23, 2004
J Anderson
Jan 23, 2004
John Reimer
Jan 23, 2004
Matthew
Jan 23, 2004
Matthew
Jan 23, 2004
J Anderson
Jan 23, 2004
John Reimer
Jan 23, 2004
John Reimer
Jan 23, 2004
Matthew
Jan 23, 2004
John Reimer
Jan 23, 2004
Phill
Jan 24, 2004
Matthew
Jan 24, 2004
Phill
Jan 24, 2004
Matthew
Jan 24, 2004
J Anderson
Jan 24, 2004
Matthew
Jan 24, 2004
J Anderson
Jan 24, 2004
Matthew
Jan 24, 2004
J Anderson
Jan 24, 2004
Phill
January 23, 2004
I'm trying to figure out how to do the equivalent of Java instanceof in D. Is anybody familiar with how to do it? It should be possible.

sample:

java :

class Square {
	int x,y;

	public boolean equals (Object object) {
		if (object instanceof Square) {
			Square p = (Square)object;
			p.x = this.x;
			p.y = this.y;
			return true;
		}
		return false;
	}
}
January 23, 2004
> 
> class Square {
> 	int x,y;
> 
> 	public boolean equals (Object object) {
> 		if (object instanceof Square) {
> 			Square p = (Square)object;
> 			p.x = this.x;
> 			p.y = this.y;
> 			return true;
> 		}
> 		return false;
> 	}
> 	}

A little mistake here, but the question still is valid. Lines with p.x, p.y, and "return true" should be replaced with the following:

return (p.x == this.x) && (p.y == this.y);
January 23, 2004
John Reimer wrote:
>>class Square {
>>	int x,y;
>>
>>	public boolean equals (Object object) {
>>		if (object instanceof Square) {
>>			Square p = (Square)object;
>>			p.x = this.x;
>>			p.y = this.y;
>>			return true;
>>		}
>>		return false;
>>	}
>>	}
> 
> 
> A little mistake here, but the question still is valid. Lines with
> p.x, p.y, and "return true" should be replaced with the following: 
> 
> return (p.x == this.x) && (p.y == this.y);


Cast to the type and check for null:

Square p = cast(Square)object;
if(p) { is instance } else { is not }
January 23, 2004
> 
> Cast to the type and check for null:
> 
> Square p = cast(Square)object;
> if(p) { is instance } else { is not }

Hmmm... that's it?

Much appreciated! :)

January 23, 2004
If cast() fails, it will return null?  Or do you need a try/catch?

Vathix wrote:

> John Reimer wrote:
> 
>>> class Square {
>>>     int x,y;
>>>
>>>     public boolean equals (Object object) {
>>>         if (object instanceof Square) {
>>>             Square p = (Square)object;
>>>             p.x = this.x;
>>>             p.y = this.y;
>>>             return true;
>>>         }
>>>         return false;
>>>     }
>>>     }
>>
>>
>>
>> A little mistake here, but the question still is valid. Lines with
>> p.x, p.y, and "return true" should be replaced with the following:
>> return (p.x == this.x) && (p.y == this.y);
> 
> 
> 
> Cast to the type and check for null:
> 
> Square p = cast(Square)object;
> if(p) { is instance } else { is not }

January 23, 2004
Brad Anderson wrote:

> If cast() fails, it will return null?  Or do you need a try/catch?
> 


Just returns null.
January 23, 2004
Vathix wrote:

> Brad Anderson wrote:
>
>> If cast() fails, it will return null?  Or do you need a try/catch?
>>
>
> Just returns null.

BTW: In C++ you'd have to use dynamic_cast<Square>(object)

-- 
-Anderson: http://badmama.com.au/~anderson/
January 23, 2004
> 
> BTW: In C++ you'd have to use dynamic_cast<Square>(object)
> 

That's an interesting point. D way seems so much simpler. :)
January 23, 2004
> >
> > BTW: In C++ you'd have to use dynamic_cast<Square>(object)
> >
>
> That's an interesting point. D way seems so much simpler. :)

How do you work that? It's the same number of expressions/line. And the C++ is *far* clearer in highlighting that something nasty is happening by the intentionally ugly dynamic_cast<>




January 23, 2004
Brad Anderson says

> If cast() fails, it will return null?  Or do you need a try/catch?

Maybe a construct can be made that supports throwing an exception. This would be in the form:

try {
<instance2> = safeCast(<class2>)<instance1>;
} catch (ClassCastException e) {
..
}

Though it could also be done using a template I think.

Regards,
Sjoerd van Leent


« First   ‹ Prev
1 2 3 4