Thread overview
More type-flexible arrays?
Jun 14, 2015
Ozan
Jun 14, 2015
ketmar
Jun 15, 2015
Ozan
Jun 14, 2015
ketmar
Jun 14, 2015
Ali Çehreli
Jun 15, 2015
Ozan
Jun 14, 2015
John Colvin
Jun 14, 2015
Marc Schütz
Jun 16, 2015
thedeemon
June 14, 2015
Hallo!

Is it possible to create arrays which has more then one type,
f. ex. array[0] = 1; array[1] = "z"; array[2] = new clazz(), ....

I tried "Variant", but it slow down heavily my app.

Greetings,
Ozan
June 14, 2015
On Sun, 14 Jun 2015 06:12:29 +0000, Ozan wrote:

> Hallo!
> 
> Is it possible to create arrays which has more then one type,
> f. ex. array[0] = 1; array[1] = "z"; array[2] = new clazz(), ....
> 
> I tried "Variant", but it slow down heavily my app.

it is possible. with Variant. ;-)

chances are that you're doing something by the wrong way in your code. not syntactically wrong, but algorithmically wrong. why to you need such an array? weak typing is slow. strong typing is fast. seems that you are used to scripting languages with weak typing, and designed your algorithm with weak typing in mind. i think it's better to try to redesign your algorithm, not looking for faster weak typing solution.

June 14, 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

June 14, 2015
p.s. faster weak typing solution is definitely possible if you'll narrow possible type set. but as i said, i'd not recommend to go this way. there can be dragon in the end.

June 14, 2015
On Sunday, 14 June 2015 at 06:12:30 UTC, Ozan wrote:
> Hallo!
>
> Is it possible to create arrays which has more then one type,
> f. ex. array[0] = 1; array[1] = "z"; array[2] = new clazz(), ....
>
> I tried "Variant", but it slow down heavily my app.
>
> Greetings,
> Ozan

It's always going to be slower. To do this, every access to the array has to come with some amount of branching and/or and indirection (including an indirect function call if you do it the OOP way), which all have a cost, not to mention obstructing optimisation by the compiler.

It's possible that std.variant.Variant isn't as fast as it could be, but it's never going to be even close to zero-performance-cost.
June 14, 2015
On Sunday, 14 June 2015 at 06:12:30 UTC, Ozan wrote:
> Hallo!
>
> Is it possible to create arrays which has more then one type,
> f. ex. array[0] = 1; array[1] = "z"; array[2] = new clazz(), ....
>
> I tried "Variant", but it slow down heavily my app.
>
> Greetings,
> Ozan

You can try Algebraic instead (also in std.variant). It's possible that it's faster, but OTOH, you need to specify the list of types it can accept.
June 15, 2015
On Sunday, 14 June 2015 at 06:43:48 UTC, ketmar wrote:
> On Sun, 14 Jun 2015 06:12:29 +0000, Ozan wrote:
>
>> Hallo!
>> 
>> Is it possible to create arrays which has more then one type,
>> f. ex. array[0] = 1; array[1] = "z"; array[2] = new clazz(), ....
>> 
>> I tried "Variant", but it slow down heavily my app.
>
> it is possible. with Variant. ;-)
>
> chances are that you're doing something by the wrong way in your code. not syntactically wrong, but algorithmically wrong. why to you need such an array? weak typing is slow. strong typing is fast. seems that you are used to scripting languages with weak typing, and designed your algorithm with weak typing in mind. i think it's better to try to redesign your algorithm, not looking for faster weak typing solution.

Thanks for your advise.
You read my mind. It's a strong Groovy background.
I think with D, I'm on right way to solve my personal problems ;-)

Regards Ozan
June 15, 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
June 16, 2015
On Sunday, 14 June 2015 at 06:12:30 UTC, Ozan wrote:

> Is it possible to create arrays which has more then one type,
> f. ex. array[0] = 1; array[1] = "z"; array[2] = new clazz(),

Probably tuples are what you really need here.
http://dlang.org/phobos/std_typecons.html#.Tuple