digitalmars.D.learn - More type-flexible arrays?
- Ozan (6/6) Jun 13 2015 Hallo!
- ketmar (8/14) Jun 13 2015 it is possible. with Variant. ;-)
- Ozan (5/21) Jun 15 2015 Thanks for your advise.
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (23/27) Jun 13 2015 Another option is OOP. Depending on the way they are used in the
- Ozan (6/37) Jun 15 2015 Thanks.
- ketmar (3/3) Jun 13 2015 p.s. faster weak typing solution is definitely possible if you'll narrow...
- John Colvin (9/16) Jun 14 2015 It's always going to be slower. To do this, every access to the
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (4/11) Jun 14 2015 You can try Algebraic instead (also in std.variant). It's
- thedeemon (3/5) Jun 16 2015 Probably tuples are what you really need here.
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
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
On Sunday, 14 June 2015 at 06:43:48 UTC, ketmar wrote:On Sun, 14 Jun 2015 06:12:29 +0000, Ozan wrote: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 OzanHallo! 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.
Jun 15 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
Jun 13 2015
On Sunday, 14 June 2015 at 06:46:05 UTC, Ali Çehreli wrote:On 06/13/2015 11:12 PM, Ozan wrote: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 OzanHallo! 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 15 2015
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
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, OzanIt'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
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, OzanYou 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
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