Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
April 20, 2007 Runtime class check | ||||
---|---|---|---|---|
| ||||
Is there a way to have a runtime class check other than if(cast(X)y) ( It just does not look really "nice" to me ^^) |
April 20, 2007 Re: Runtime class check | ||||
---|---|---|---|---|
| ||||
Posted in reply to funog | funog wrote:
> Is there a way to have a runtime class check other than
> if(cast(X)y)
> ( It just does not look really "nice" to me ^^)
>
Unless you are not going to use it as the type you are checking for what's wrong with that (or this)
if(auto x = cast(X) y)
{
// use x
}
|
April 20, 2007 Re: Runtime class check | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | BCS Wrote:
> funog wrote:
> > Is there a way to have a runtime class check other than
> > if(cast(X)y)
> > ( It just does not look really "nice" to me ^^)
> >
>
> Unless you are not going to use it as the type you are checking for what's wrong with that (or this)
>
> if(auto x = cast(X) y)
> {
> // use x
> }
You are right, it's nice. But i guess it means the answer to my question is "no" ;)
|
April 20, 2007 Re: Runtime class check | ||||
---|---|---|---|---|
| ||||
Posted in reply to funog | funog wrote:
> BCS Wrote:
>
>> funog wrote:
>>> Is there a way to have a runtime class check other than
>>> if(cast(X)y)
>>> ( It just does not look really "nice" to me ^^)
>>>
>> Unless you are not going to use it as the type you are checking for what's wrong with that (or this)
>>
>> if(auto x = cast(X) y)
>> {
>> // use x
>> }
>
> You are right, it's nice. But i guess it means the answer to my question is "no" ;)
Not strictly speaking, but certainly nothing more elegant. You could presumably, for example, compare the .classinfo referances -- if (y.classinfo is X.classinfo) -- but... ew. :)
-- Chris Nicholson-Sauls
|
April 21, 2007 Re: Runtime class check | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Nicholson-Sauls | Chris Nicholson-Sauls wrote: > funog wrote: >> BCS Wrote: >> >>> funog wrote: >>>> Is there a way to have a runtime class check other than >>>> if(cast(X)y) >>>> ( It just does not look really "nice" to me ^^) >>>> >>> Unless you are not going to use it as the type you are checking for >>> what's wrong with that (or this) >>> >>> if(auto x = cast(X) y) >>> { >>> // use x >>> } >> >> You are right, it's nice. But i guess it means the answer to my question is "no" ;) > > Not strictly speaking, but certainly nothing more elegant. You could presumably, for example, compare the .classinfo referances -- if (y.classinfo is X.classinfo) -- but... ew. :) > > -- Chris Nicholson-Sauls Amusing anecdote: when I first started using D, I was under the impression that cast(T) would throw an exception if the cast couldn't be done. So, off I went to write an instanceof function. A few days later, I had this hideous piece of code that went and walked the object's inheritance tree to find out if it could be cast to the given type. Almost immediately after I finished, I read something on the NG that pointed out that cast(T) simply returns null if the cast failed. Fiddlesticks. Morale of this story: read the docs before you go assuming things :3 -- Daniel -- int getRandomNumber() { return 4; // chosen by fair dice roll. // guaranteed to be random. } http://xkcd.com/ v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/ |
Copyright © 1999-2021 by the D Language Foundation