More type-flexible arrays?

Ozan via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jun 15 02:17:42 PDT 2015


On Sunday, 14 June 2015 at 06:46:05 UTC, Ali Çehreli wrote:
> On 06/13/2015 11:12 PM, Ozan wrote:
>
> > Hallo!
> >
> > Is it possible to create arrays which has more then one type,
>
> Not possible.
>
> > f. ex. array[0] = 1; array[1] = "z"; array[2] = new clazz(),
> ....
> >
> > I tried "Variant", but it slow down heavily my app.
>
> Another option is OOP. Depending on the way they are used in 
> the program, those different types have a common interface. It 
> is possible to have an array of that interface:
>
>     Foo[] foos;
>
> interface Foo
> {
>     void commonFunction();
>     // ...
> }
>
> class clazz : Foo
> {
>     // ...
> }
>
> However, fundamental types like int need to be boxed:
>
> class FundamentalFoo(T) : Foo
> {
>     T value;
>
>     // ...
> }
>
> If needed, you can specialize FundamentalFoo for int, string, 
> etc.
>
> Ali

Thanks.
It need some effort but it is a more performant to have a 
multi-type array compared to Variants. And thanks to D's template 
system it isn't so much effort.

Regards Ozan


More information about the Digitalmars-d-learn mailing list