www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - cast a LinkSeq

reply Qian Xu <quian.xu stud.tu-ilmenau.de> writes:
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
Apr 06 2009
parent reply Adam Burton <adz21c googlemail.com> writes:
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
Apr 06 2009
parent reply Qian Xu <quian.xu stud.tu-ilmenau.de> writes:
Adam Burton wrote:
 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.
 
yes. I can cast all Apple-object to Fruit-objects one by one. I hope there is an one-line-solution :-) -- Xu, Qian (stanleyxu) http://stanleyxu2005.blogspot.com
Apr 06 2009
parent Daniel Keep <daniel.keep.lists gmail.com> writes:
Qian Xu wrote:
 Adam Burton wrote:
 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.

 
 yes. I can cast all Apple-object to Fruit-objects one by one. I hope
 there is an one-line-solution :-)
You can't do it. Imagine you cast your LinkSeq!(Apple) to LinkSeq!(Fruit). You can now add a Banana to your LinkSeq!(Fruit), thus corrupting the original object. You get a similar problem with arrays. The most direct way would probably be to create a LinkSeqView!(T) class which did the cast on the fly and prohibited mutating operations. -- Daniel
Apr 06 2009