Thread overview
Testing derived type
Jul 05, 2008
Nick Sabalausky
Jul 05, 2008
Nick Sabalausky
Jul 05, 2008
Kirk McDonald
Jul 05, 2008
Koroskin Denis
Jul 05, 2008
Koroskin Denis
Jul 05, 2008
Frank Benoit
Jul 06, 2008
Nick Sabalausky
July 05, 2008
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 */ }
}


July 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 */ }
}



July 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..$] !=
> derivedA.stringof)
>     { /* do stuff */ }
> }
>
>

Maybe this?

...
if ( (cast(derivedA)elem) is null ) {
	/* do stuff */
}
...
July 05, 2008
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
July 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:
>
>> 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 */ }
>> }
>>
>>
>
> 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).
July 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 */
}

July 06, 2008
"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 ;) )