www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - More type-flexible arrays?

reply "Ozan" <ozan.sueel gmail.com> writes:
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
Jun 13 2015
next sibling parent reply ketmar <ketmar ketmar.no-ip.org> writes:
On Sun, 14 Jun 2015 06:12:29 +0000, Ozan wrote:

 Hallo!
=20
 Is it possible to create arrays which has more then one type,
 f. ex. array[0] =3D 1; array[1] =3D "z"; array[2] =3D new clazz(), ....
=20
 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.=20 not syntactically wrong, but algorithmically wrong. why to you need such=20 an array? weak typing is slow. strong typing is fast. seems that you are=20 used to scripting languages with weak typing, and designed your algorithm=20 with weak typing in mind. i think it's better to try to redesign your=20 algorithm, not looking for faster weak typing solution.=
Jun 13 2015
parent "Ozan" <ozan.sueel gmail.com> writes:
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
Jun 15 2015
prev sibling next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Jun 13 2015
parent "Ozan" <ozan.sueel gmail.com> writes:
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
Jun 15 2015
prev sibling next sibling parent ketmar <ketmar ketmar.no-ip.org> writes:
p.s. faster weak typing solution is definitely possible if you'll narrow=20
possible type set. but as i said, i'd not recommend to go this way. there=20
can be dragon in the end.=
Jun 13 2015
prev sibling next sibling parent "John Colvin" <john.loughran.colvin gmail.com> writes:
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.
Jun 14 2015
prev sibling next sibling parent "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
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.
Jun 14 2015
prev sibling parent "thedeemon" <dlang thedeemon.com> writes:
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.
Jun 16 2015