digitalmars.D.learn - Testing derived type
- Nick Sabalausky (15/15) Jul 05 2008 I assume there's some way to do the following (D1 and Tango) without
- Nick Sabalausky (16/31) Jul 05 2008 To clarify, what I'm looking for is this):
- Kirk McDonald (9/49) Jul 05 2008 Attempt to cast it to a derivedA. If it isn't actually an instance of
- Koroskin Denis (7/22) Jul 05 2008 Maybe this?
- Koroskin Denis (9/35) Jul 05 2008 Or use
- Frank Benoit (5/14) Jul 05 2008 you can reuse the casted ref in the same if:
- Nick Sabalausky (4/18) Jul 05 2008 Ahh, thanks everyone. Sometimes the obvious excapes me (as my boolean x...
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
"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
Nick Sabalausky wrote:"Nick Sabalausky" <a a.a> wrote in message news:g4onj4$1cbf$1 digitalmars.com...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.orgI 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
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
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:Or use ... if ( elem.classinfo !is derivedA.classinfo ) { // do stuff } ... to skip derivedA instances only (and not derivatives).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
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
"Frank Benoit" <keinfarbton googlemail.com> wrote in message news:g4opif$1fmn$1 digitalmars.com...Koroskin Denis schrieb:Ahh, thanks everyone. Sometimes the obvious excapes me (as my boolean xor operator proposal in a different thread showed ;) )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