Thread overview | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 23, 2004 Java instanceof? | ||||
---|---|---|---|---|
| ||||
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 Re: Java instanceof? | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Reimer | >
> 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 Re: Java instanceof? | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Reimer | 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 Re: Java instanceof? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vathix | >
> 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 Re: Java instanceof? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vathix | 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 Re: Java instanceof? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brad Anderson | Brad Anderson wrote:
> If cast() fails, it will return null? Or do you need a try/catch?
>
Just returns null.
|
January 23, 2004 Re: Java instanceof? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vathix | 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 Re: Java instanceof? | ||||
---|---|---|---|---|
| ||||
Posted in reply to J Anderson | >
> 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 Re: Java instanceof? | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Reimer | > >
> > 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 Re: Java instanceof? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vathix | 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
|
Copyright © 1999-2021 by the D Language Foundation