www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Generic array

reply RenatoL <rexlen gmail.com> writes:
##[3] arr = [0, "aa", 2.4];

What can i put instead of ##?

In C#, just for example, i can write:

object[] ar1 = new object[3];
ar1[0] = 1;
ar1[1] = "hello";
ar1[2] = 'a';

and it works. But in D

Object[3] arr0 = [0, "aa", 2.4];

and  compiler complains....
Nov 15 2011
next sibling parent bioinfornatics <bioinfornatics fedoraproject.rog> writes:
Le mardi 15 novembre 2011 =C3=A0 23:15 +0000, RenatoL a =C3=A9crit :
 ##[3] arr =3D [0, "aa", 2.4];
=20
 What can i put instead of ##?
=20
 In C#, just for example, i can write:
=20
 object[] ar1 =3D new object[3];
 ar1[0] =3D 1;
 ar1[1] =3D "hello";
 ar1[2] =3D 'a';
=20
 and it works. But in D
=20
 Object[3] arr0 =3D [0, "aa", 2.4];
=20
 and  compiler complains....

this works: -------------------------------------------------- import std.string; import std.variant; import std.stdio; void main( string[] args ){ Variant[] array =3D [ cast(Variant)1u , cast(Variant)"hi", cast(Variant)-2, cast(Variant)'5' ];=20 foreach( var; array ){ writefln( "type: %s, value: %s", var.type, var ); } } -------------------------------------------------- Output: -------------------------------------------------- type: uint, value: 1 type: immutable(char)[], value: hi type: int, value: -2 type: char, value: 5 --------------------------------------------------
Nov 15 2011
prev sibling next sibling parent Jesse Phillips <jessekphillips+d gmail.com> writes:
On Wed, 16 Nov 2011 00:30:44 +0100, bioinfornatics wrote:

 this works: --------------------------------------------------
 import std.string;
 import std.variant;
 import std.stdio;
 
 void main( string[] args ){
     Variant[] array = [ cast(Variant)1u , cast(Variant)"hi",
 cast(Variant)-2, cast(Variant)'5' ];
     foreach( var; array ){
         writefln( "type: %s, value: %s", var.type, var );
     }
 }

Not testing my suggestion, but you should be able to replace all casts with Variant() Variant(1u)...
Nov 15 2011
prev sibling next sibling parent reply Dejan Lekic <dejan.lekic gmail.com> writes:
RenatoL wrote:

 ##[3] arr = [0, "aa", 2.4];
 
 What can i put instead of ##?
 
 In C#, just for example, i can write:
 
 object[] ar1 = new object[3];
 ar1[0] = 1;
 ar1[1] = "hello";
 ar1[2] = 'a';
 
 and it works. But in D
 
 Object[3] arr0 = [0, "aa", 2.4];
 
 and  compiler complains....

In D, the equivalent would be: Variant[] arr = variantArray(0, "aa", 2.4);
Nov 16 2011
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Wednesday, November 16, 2011 08:33:04 Dejan Lekic wrote:
 RenatoL wrote:
 ##[3] arr = [0, "aa", 2.4];
 
 What can i put instead of ##?
 
 In C#, just for example, i can write:
 
 object[] ar1 = new object[3];
 ar1[0] = 1;
 ar1[1] = "hello";
 ar1[2] = 'a';
 
 and it works. But in D
 
 Object[3] arr0 = [0, "aa", 2.4];
 
 and  compiler complains....

In D, the equivalent would be: Variant[] arr = variantArray(0, "aa", 2.4);

Yeah. Only classes can be cast to Object in D. There is no autoboxing or the like. There is no common type for everything. Variant is a struct which can hold any type thanks to a union, but it's not a common type like object essentially is in C#. - Jonathan M Davis
Nov 16 2011
parent RenatoL <rexlen gmail.com> writes:
Ok, tk u all.
I guess this is a very poor approach if we are looking for
performance
Nov 17 2011
prev sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Thursday, November 17, 2011 14:41 RenatoL wrote:
 Ok, tk u all.
 I guess this is a very poor approach if we are looking for
 performance

Mixing types like that in an array is not a normal thing to do. However, if you're looking to hold a specific number of items of diverse types (particularly if there's only a few of them), you can look at std.typecons.Tuple. In that case, you'd do something like auto t = tuple(0, "aa", 2.4); And you don't have to query about the type information that way, because it's part of the type - Tuple!(int, string, float). Then you can index like you would an array. auto a = t[0]; auto b = t[1]; auto c = t[2]; - Jonathan M Davis
Nov 17 2011