More type-flexible arrays?

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jun 13 23:46:04 PDT 2015


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



More information about the Digitalmars-d-learn mailing list