cast a LinkSeq

Adam Burton adz21c at googlemail.com
Mon Apr 6 10:26:17 PDT 2009


I wouldn't think so, cos LinkSeq!(Apple) does not inherit LinkSeq!(Fruit), 
they are 2 separate types. However your apples automatically downcast (or 
up, depending which way you like to draw your diagrams :-) ) so unless you  
intend to pass the LinkSeq!(Apple) into a function expecting LinkSeq!(Fruit)  
it shouldn't be a problem. If you are passing about LinqSeq!(Fruit) and want  
your LinkSeq!(Apple) to fit you might need to write some adapters and make 
use of the models available to you or something along them lines.

That's my understanding anyway.

Qian Xu wrote:

> Hi All,
> 
> can I cast a LinkSeq from inherited type to base type?
> 
> ------------------------ code --------------------------
>    class Fruit {}
>    class Apple: Fruit {}
> 
>    auto apples = new LinkSeq!(Apple);
>    apples.append(new Apple);
>    assert(apples !is null);
>    assert(apples.length == 1);
>    
>    auto fruits = cast(LinkSeq!(Fruit))(apples);
>    assert(fruits !is null); // <--- failed
>    assert(fruits.length == 1);
> ------------------------ code --------------------------
> 
> 
> --Qian




More information about the Digitalmars-d-learn mailing list