January 24, 2004 Re: Java instanceof? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew | Matthew wrote: >"J Anderson" <REMOVEanderson@badmama.com.au> wrote in message >news:bush92$2akt$1@digitaldaemon.com... > > >>Phill wrote: >> >> >> >>>"Matthew" <matthew.hat@stlsoft.dot.org> wrote in message >>>news:busegu$26ce$1@digitaldaemon.com... >>> >>> >>> >>> >>>>>I dont see how testing to see if an Object >>>>>is an instanceof another Object BEFORE >>>>>downcasting can be bad in anyway, in fact, its >>>>>a bit like wearing a condom isnt it? >>>>> >>>>> >>>>> >>>>> >>>>There are two separate issues that you're commenting on here. If you're >>>>going to downcast, then it's certainly better to avoid doing a check >>>> >>>> >>>> >>>> >>>before >>> >>> >>> >>> >>>>and to have the success of the cast provide the information you require. >>>> >>>> >>>> >>>> >>>Sorry I meant it is good to check before, IF you are going to downcast. I >>>cant >>>see a problem with doing it this way can you? >>> >>>================ >>>if(obj instanceof(Button)) >>> then downcast; >>>else >>> dont downcast; >>>-------------------------- >>> >>>Phill. >>> >>> >>> >>> >>> >>> >>> >>> >>IF your going to downcast your going to test, why not do it in one go? >> >>Button but = cast(Button)obj; >>if (but) >>{} >> >>99% of the time your going to need to cast down if you need to test the >>type (please don't take that as advocating down casting) and your not >>saving any performance by checking first. Futhermore I hope this is not >>an argument for a new term, because if your only testing for type you >>can go: >> >>if (cast(Button)obj) >>{} >> >> > >I think if we're entertaining the notion of opExplicitCast, and even if >we're not, we really need to have a separate operator for downcast, e.g. > > if (downcast(Button)obj) > {} > > > >That way any time you downcast readers of your code will know that you were >downcast at the time. He he. (Apologies to any non-English / non-pompous >readers. <g>) > > > Furthermore I hope this is not an argument for a new term. <- What I meant by that was instanceof is a different from casting, and less powerful. You could have downcast though as it's just a variant on cast. <g> Even being an Australian English speaker I trouble understanding you some times ;) -- -Anderson: http://badmama.com.au/~anderson/ |
January 24, 2004 Re: Java instanceof? | ||||
---|---|---|---|---|
| ||||
Posted in reply to J Anderson | Sorry guys I was thinking in Java In Java as you know you cant if(Object) it takes and returns a boolean value true or false. So probably a quicker way than the first way I mentioned, would be to downcast in a try and catch the exception if it occurs. non-pompous Aussie. :o) "J Anderson" <REMOVEanderson@badmama.com.au> wrote in message news:bush92$2akt$1@digitaldaemon.com... > Phill wrote: > > >"Matthew" <matthew.hat@stlsoft.dot.org> wrote in message news:busegu$26ce$1@digitaldaemon.com... > > > > > >>>I dont see how testing to see if an Object > >>>is an instanceof another Object BEFORE > >>>downcasting can be bad in anyway, in fact, its > >>>a bit like wearing a condom isnt it? > >>> > >>> > >>There are two separate issues that you're commenting on here. If you're going to downcast, then it's certainly better to avoid doing a check > >> > >> > >before > > > > > >>and to have the success of the cast provide the information you require. > >> > >> > > > >Sorry I meant it is good to check before, IF you are going to downcast. I > >cant > >see a problem with doing it this way can you? > > > >================ > >if(obj instanceof(Button)) > > then downcast; > >else > > dont downcast; > >-------------------------- > > > >Phill. > > > > > > > > > > > > > IF your going to downcast your going to test, why not do it in one go? > > Button but = cast(Button)obj; > if (but) > {} > > 99% of the time your going to need to cast down if you need to test the type (please don't take that as advocating down casting) and your not saving any performance by checking first. Futhermore I hope this is not an argument for a new term, because if your only testing for type you can go: > > if (cast(Button)obj) > {} > > -- > -Anderson: http://badmama.com.au/~anderson/ |
January 24, 2004 Re: Java instanceof? | ||||
---|---|---|---|---|
| ||||
Posted in reply to J Anderson | "J Anderson" <REMOVEanderson@badmama.com.au> wrote in message news:busj7q$2dlj$1@digitaldaemon.com... > Matthew wrote: > > >"J Anderson" <REMOVEanderson@badmama.com.au> wrote in message news:bush92$2akt$1@digitaldaemon.com... > > > > > >>Phill wrote: > >> > >> > >> > >>>"Matthew" <matthew.hat@stlsoft.dot.org> wrote in message news:busegu$26ce$1@digitaldaemon.com... > >>> > >>> > >>> > >>> > >>>>>I dont see how testing to see if an Object > >>>>>is an instanceof another Object BEFORE > >>>>>downcasting can be bad in anyway, in fact, its > >>>>>a bit like wearing a condom isnt it? > >>>>> > >>>>> > >>>>> > >>>>> > >>>>There are two separate issues that you're commenting on here. If you're > >>>>going to downcast, then it's certainly better to avoid doing a check > >>>> > >>>> > >>>> > >>>> > >>>before > >>> > >>> > >>> > >>> > >>>>and to have the success of the cast provide the information you require. > >>>> > >>>> > >>>> > >>>> > >>>Sorry I meant it is good to check before, IF you are going to downcast. I > >>>cant > >>>see a problem with doing it this way can you? > >>> > >>>================ > >>>if(obj instanceof(Button)) > >>> then downcast; > >>>else > >>> dont downcast; > >>>-------------------------- > >>> > >>>Phill. > >>> > >>> > >>> > >>> > >>> > >>> > >>> > >>> > >>IF your going to downcast your going to test, why not do it in one go? > >> > >>Button but = cast(Button)obj; > >>if (but) > >>{} > >> > >>99% of the time your going to need to cast down if you need to test the type (please don't take that as advocating down casting) and your not saving any performance by checking first. Futhermore I hope this is not an argument for a new term, because if your only testing for type you can go: > >> > >>if (cast(Button)obj) > >>{} > >> > >> > > > >I think if we're entertaining the notion of opExplicitCast, and even if we're not, we really need to have a separate operator for downcast, e.g. > > > > if (downcast(Button)obj) > > {} > > > > > > > >That way any time you downcast readers of your code will know that you were > >downcast at the time. He he. (Apologies to any non-English / non-pompous > >readers. <g>) > > > > > > > Furthermore I hope this is not an argument for a new term. <- What I meant by that was instanceof is a different from casting, and less powerful. You could have downcast though as it's just a variant on cast. Not sure what you mean. Can you rephrase? > <g> Even being an Australian English speaker I trouble understanding you some times ;) Such is my burden. :) > -- > -Anderson: http://badmama.com.au/~anderson/ |
January 24, 2004 Re: Java instanceof? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew | Matthew wrote: >>> >>> >>Furthermore I hope this is not an argument for a new term. <- What I meant >> >> >by that was instanceof is a different from casting, and less powerful. You >could have downcast though as it's just a variant on cast. > >Not sure what you mean. Can you rephrase? > > > with obj instance of(Square) All you get back is a Boolean value. It's different from casting, with different rules and form. cast(Square) obj; downcast(Square) obj; The form is the same, and the meaning is almost the same, it's really only a change of words. Of course they wouldn't be interchangeable in most cases but downcast less of a burden on the programmer to learn then instanceof, which is also less functional. >><g> Even being an Australian English speaker I trouble understanding you >>some times ;) >> >> > >Such is my burden. :) > > > > >>-- >>-Anderson: http://badmama.com.au/~anderson/ >> >> -- -Anderson: http://badmama.com.au/~anderson/ |
Copyright © 1999-2021 by the D Language Foundation