www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Testing derived type

reply "Nick Sabalausky" <a a.a> writes:
I assume there's some way to do the following (D1 and Tango) without 
converting to and comparing strings, but...how?

module modA;
class base {}
class derivedA : base {}
class derivedB : base {}
class derivedC : base {}

module modB;
base[] array;
foreach(base elem; array)
{
    if(elem.toString[locatePrior(elem.toString, '.')+1..$] != 
derivedA.stringof)
    { /* do stuff */ }
}
Jul 05 2008
next sibling parent reply "Nick Sabalausky" <a a.a> writes:
"Nick Sabalausky" <a a.a> wrote in message 
news:g4onj4$1cbf$1 digitalmars.com...
I assume there's some way to do the following (D1 and Tango) without 
converting to and comparing strings, but...how?

 module modA;
 class base {}
 class derivedA : base {}
 class derivedB : base {}
 class derivedC : base {}

 module modB;
 base[] array;
 foreach(base elem; array)
 {
    if(elem.toString[locatePrior(elem.toString, '.')+1..$] != 
 derivedA.stringof)
    { /* do stuff */ }
 }
To clarify, what I'm looking for is this): module modA; class base {} class derivedA : base {} class derivedB : base {} class derivedC : base {} module modB; base[] array; foreach(base elem; array) { // Do this without converting to and comparing strings if(/* elem is not an instance of 'derivedA' */) { /* do stuff */ } }
Jul 05 2008
parent Kirk McDonald <kirklin.mcdonald gmail.com> writes:
Nick Sabalausky wrote:
 "Nick Sabalausky" <a a.a> wrote in message 
 news:g4onj4$1cbf$1 digitalmars.com...
 I assume there's some way to do the following (D1 and Tango) without 
 converting to and comparing strings, but...how?

 module modA;
 class base {}
 class derivedA : base {}
 class derivedB : base {}
 class derivedC : base {}

 module modB;
 base[] array;
 foreach(base elem; array)
 {
    if(elem.toString[locatePrior(elem.toString, '.')+1..$] != 
 derivedA.stringof)
    { /* do stuff */ }
 }
To clarify, what I'm looking for is this): module modA; class base {} class derivedA : base {} class derivedB : base {} class derivedC : base {} module modB; base[] array; foreach(base elem; array) { // Do this without converting to and comparing strings if(/* elem is not an instance of 'derivedA' */) { /* do stuff */ } }
Attempt to cast it to a derivedA. If it isn't actually an instance of derivedA (or of a subclass of derivedA), the cast will return null. (Just like dynamic_cast in C++.) -- Kirk McDonald http://kirkmcdonald.blogspot.com Pyd: Connecting D and Python http://pyd.dsource.org
Jul 05 2008
prev sibling parent reply "Koroskin Denis" <2korden gmail.com> writes:
On Sun, 06 Jul 2008 01:03:33 +0400, Nick Sabalausky <a a.a> wrote:

 I assume there's some way to do the following (D1 and Tango) without
 converting to and comparing strings, but...how?

 module modA;
 class base {}
 class derivedA : base {}
 class derivedB : base {}
 class derivedC : base {}

 module modB;
 base[] array;
 foreach(base elem; array)
 {
     if(elem.toString[locatePrior(elem.toString, '.')+1..$] !=3D
 derivedA.stringof)
     { /* do stuff */ }
 }
Maybe this? ... if ( (cast(derivedA)elem) is null ) { /* do stuff */ } ...
Jul 05 2008
parent reply "Koroskin Denis" <2korden gmail.com> writes:
On Sun, 06 Jul 2008 01:11:52 +0400, Koroskin Denis <2korden gmail.com>  =

wrote:

 On Sun, 06 Jul 2008 01:03:33 +0400, Nick Sabalausky <a a.a> wrote:

 I assume there's some way to do the following (D1 and Tango) without
 converting to and comparing strings, but...how?

 module modA;
 class base {}
 class derivedA : base {}
 class derivedB : base {}
 class derivedC : base {}

 module modB;
 base[] array;
 foreach(base elem; array)
 {
     if(elem.toString[locatePrior(elem.toString, '.')+1..$] !=3D
 derivedA.stringof)
     { /* do stuff */ }
 }
Maybe this? ... if ( (cast(derivedA)elem) is null ) { /* do stuff */ } ...
Or use ... if ( elem.classinfo !is derivedA.classinfo ) { // do stuff } ... to skip derivedA instances only (and not derivatives).
Jul 05 2008
parent reply Frank Benoit <keinfarbton googlemail.com> writes:
Koroskin Denis schrieb:

 Or use
 
 ....
 if ( elem.classinfo !is derivedA.classinfo ) {
     // do stuff
 }
 ....
 
 to skip derivedA instances only (and not derivatives).
you can reuse the casted ref in the same if: if ( auto o = cast(derivedA)elem ) { /* do stuff, use o */ }
Jul 05 2008
parent "Nick Sabalausky" <a a.a> writes:
"Frank Benoit" <keinfarbton googlemail.com> wrote in message 
news:g4opif$1fmn$1 digitalmars.com...
 Koroskin Denis schrieb:

 Or use

 ....
 if ( elem.classinfo !is derivedA.classinfo ) {
     // do stuff
 }
 ....

 to skip derivedA instances only (and not derivatives).
you can reuse the casted ref in the same if: if ( auto o = cast(derivedA)elem ) { /* do stuff, use o */ }
Ahh, thanks everyone. Sometimes the obvious excapes me (as my boolean xor operator proposal in a different thread showed ;) )
Jul 05 2008