Thread overview | ||||||
---|---|---|---|---|---|---|
|
April 06, 2009 cast a LinkSeq | ||||
---|---|---|---|---|
| ||||
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 |
April 06, 2009 Re: cast a LinkSeq | ||||
---|---|---|---|---|
| ||||
Posted in reply to Qian Xu | 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
|
April 06, 2009 Re: cast a LinkSeq | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam Burton | 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 |
April 06, 2009 Re: cast a LinkSeq | ||||
---|---|---|---|---|
| ||||
Posted in reply to Qian Xu |
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
|
Copyright © 1999-2021 by the D Language Foundation