digitalmars.D.learn - Runtime class check
- funog (3/3) Apr 20 2007 Is there a way to have a runtime class check other than
- BCS (7/11) Apr 20 2007 Unless you are not going to use it as the type you are checking for
- funog (2/15) Apr 20 2007 You are right, it's nice. But i guess it means the answer to my question...
- Chris Nicholson-Sauls (5/21) Apr 20 2007 Not strictly speaking, but certainly nothing more elegant. You could pr...
- Daniel Keep (21/45) Apr 20 2007 Amusing anecdote: when I first started using D, I was under the
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 ^^)
Apr 20 2007
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 }
Apr 20 2007
BCS Wrote:funog wrote:You are right, it's nice. But i guess it means the answer to my question is "no" ;)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 }
Apr 20 2007
funog wrote:BCS Wrote: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-Saulsfunog wrote:You are right, it's nice. But i guess it means the answer to my question is "no" ;)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 }
Apr 20 2007
Chris Nicholson-Sauls wrote:funog wrote: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/BCS Wrote: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-Saulsfunog wrote:You are right, it's nice. But i guess it means the answer to my question is "no" ;)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 }
Apr 20 2007