January 23, 2004 Re: Java instanceof? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew | On Fri, 23 Jan 2004 21:36:53 +1100, Matthew wrote:
>
>> > 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<>
I don't know how I worked that! It may sound amazing, but it all just came to me on some whim! :D
Apparently, from inexperience, I assumed that the D way of doing
this was simple AND safe because of some technological marvel. If this is
not the case, then you'll have to educate me. My memory likely is in bad
need of refreshing.
For some reason I thought downcasting evils were limited to the convoluted C++ language because of all it's old appendages.
Okay?
|
January 23, 2004 Re: Java instanceof? | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Reimer | John Reimer wrote: >On Fri, 23 Jan 2004 21:36:53 +1100, Matthew wrote: > > > >>>>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<> >> >> > >I don't know how I worked that! It may sound amazing, but it all just came >to me on some whim! :D > >Apparently, from inexperience, I assumed that the D way of doing >this was simple AND safe because of some technological marvel. If this is >not the case, then you'll have to educate me. My memory likely is in bad >need of refreshing. > >For some reason I thought downcasting evils were limited to the convoluted >C++ language because of all it's old appendages. > >Okay? > > > > > > A dynamic cast needs t be done at runtime (performance hit) and can change the pointer location to null. When ever you have a dynamic cast (and only use it when nessary), you'll also need to check if the variable is null. -- -Anderson: http://badmama.com.au/~anderson/ |
January 23, 2004 Re: Java instanceof? | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Reimer | In the D Language Manual Overview, it states: Features to Keep From C/C++ "Runtime Type Identification. This is partially implemented in C++; in D it is taken to its next logical step. Fully supporting it enables better garbage collection, better debugger support, more automated persistence, etc." How is it supported in D? What features enable it? Is this visible to the programmer? I haven't been able to see where the D language enables to identify the type or class at runtime. |
January 23, 2004 Re: Java instanceof? | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Reimer | John Reimer wrote: >In the D Language Manual Overview, it states: > >Features to Keep From C/C++ > >"Runtime Type Identification. This is partially implemented in C++; in D >it is taken to its next logical step. Fully supporting it enables better >garbage collection, better debugger support, more automated persistence, >etc." > >How is it supported in D? What features enable it? Is this visible to the >programmer? I haven't been able to see where the D language enables to >identify the type or class at runtime. > > You can use *ClassInfo. * ie object.classinfo.name Check out: http://www.digitalmars.com/d/phobos.html#object The only real documentation is in object.d. -- -Anderson: http://badmama.com.au/~anderson/ |
January 23, 2004 Re: Java instanceof? | ||||
---|---|---|---|---|
| ||||
Posted in reply to J Anderson |
> A dynamic cast needs t be done at runtime (performance hit) and can change the pointer location to null. When ever you have a dynamic cast (and only use it when nessary), you'll also need to check if the variable is null.
Ok. I would think it would be important to check if the variable was null anyway before casting.
In fact, checking if the pointer changed to
null was the whole point of finding out if "object" was the same class as
square. If it changes to null during the cast, then it isn't a square.
Otherwise, it is.
That's why I asked if this is the only RTTI ability that D has.
|
January 23, 2004 Re: Java instanceof? | ||||
---|---|---|---|---|
| ||||
Posted in reply to J Anderson |
> You can use *ClassInfo. *
>
> ie
> object.classinfo.name
>
> Check out:
> http://www.digitalmars.com/d/phobos.html#object
>
> The only real documentation is in object.d.
Thanks, that's what I needed to know. I did some searching on this newsgroup too and noticed some vague references to it.
|
January 23, 2004 Re: Java instanceof? | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Reimer | > On Fri, 23 Jan 2004 21:36:53 +1100, Matthew wrote: > > > > >> > 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<> > > I don't know how I worked that! It may sound amazing, but it all just came to me on some whim! :D > > Apparently, from inexperience, I assumed that the D way of doing > this was simple AND safe because of some technological marvel. If this is > not the case, then you'll have to educate me. My memory likely is in bad > need of refreshing. > > For some reason I thought downcasting evils were limited to the convoluted C++ language because of all it's old appendages. Downcasting is evil whatever the language. It always (i.e. 99.9% of the time) indicates bad design, either on the part of the programmer (in your motivating case), or the language designer (the whole of Java / .NET), and if you find yourself using it you should question whether you're in the 0.1% area of the design-quality graph. > Okay? Always okay, John, me old buddy! :) |
January 23, 2004 Re: Java instanceof? | ||||
---|---|---|---|---|
| ||||
Posted in reply to J Anderson | "J Anderson" <REMOVEanderson@badmama.com.au> wrote in message news:bur6fm$283$1@digitaldaemon.com... > John Reimer wrote: > > >In the D Language Manual Overview, it states: > > > >Features to Keep From C/C++ > > > >"Runtime Type Identification. This is partially implemented in C++; in D it is taken to its next logical step. Fully supporting it enables better garbage collection, better debugger support, more automated persistence, etc." > > > >How is it supported in D? What features enable it? Is this visible to the programmer? I haven't been able to see where the D language enables to identify the type or class at runtime. > > > > > You can use *ClassInfo. * > > ie > object.classinfo.name NO! Sorry for shouting, but there must be one accepted way of performing downcasting, so that all programmers - especially newcomers - have a single token that they must be on guard for. Otherwise, it's going to be a disgusting sorry mess, reminiscent of Java or RTTI-mad C++ (and yes, I _have_ perpetrated my fair share of evils in this, so have much blame on my own shoulders) > Check out: > http://www.digitalmars.com/d/phobos.html#object > > The only real documentation is in object.d. |
January 23, 2004 Re: Java instanceof? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew | "Matthew" <matthew.hat@stlsoft.dot.org> wrote in message news:bur8ut$6gj$2@digitaldaemon.com... > > "J Anderson" <REMOVEanderson@badmama.com.au> wrote in message news:bur6fm$283$1@digitaldaemon.com... > > John Reimer wrote: > > > > >In the D Language Manual Overview, it states: > > > > > >Features to Keep From C/C++ > > > > > >"Runtime Type Identification. This is partially implemented in C++; in D > > >it is taken to its next logical step. Fully supporting it enables better > > >garbage collection, better debugger support, more automated persistence, > > >etc." > > > > > >How is it supported in D? What features enable it? Is this visible to the > > >programmer? I haven't been able to see where the D language enables to identify the type or class at runtime. > > > > > > > > You can use *ClassInfo. * > > > > ie > > object.classinfo.name > > NO! > > Sorry for shouting, but there must be one accepted way of performing downcasting, so that all programmers - especially newcomers - have a single > token that they must be on guard for. > > Otherwise, it's going to be a disgusting sorry mess, reminiscent of Java or > RTTI-mad C++ (and yes, I _have_ perpetrated my fair share of evils in this, > so have much blame on my own shoulders) Hmm. Maybe that'll teach me not to be working in the small hours. I thought you were recommending this as a mechanism for downcasting. Re-reading this makes me wonder whether that is the case. If it is, the shout stays. If not, then I shall flagellate myself soundly on your behalf. :) Dr Proctor |
January 23, 2004 Re: Java instanceof? | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Reimer | To set people straight here. This code was a modified example of what the Java SWT GUI class does. I posted it here so I could get help on how to work the code in D (trying to port this). The real code tests using instanceof and then promptly downcasts if the object is found valid. Bad design? I really don't know if it is. But 99% chance that it is. :-) So it wasn't just me! I was just the unfortunate simpleton to choke on this candy. Later, John |
Copyright © 1999-2021 by the D Language Foundation