digitalmars.D.learn - Generic array
- RenatoL (10/10) Nov 15 2011 ##[3] arr = [0, "aa", 2.4];
- bioinfornatics (21/37) Nov 15 2011 this works:
- Jesse Phillips (4/16) Nov 15 2011 Not testing my suggestion, but you should be able to replace all casts
- Dejan Lekic (3/19) Nov 16 2011 In D, the equivalent would be:
- Jonathan M Davis (6/27) Nov 16 2011 Yeah. Only classes can be cast to Object in D. There is no autoboxing or...
- RenatoL (3/3) Nov 17 2011 Ok, tk u all.
- Jonathan M Davis (14/17) Nov 17 2011 Mixing types like that in an array is not a normal thing to do. However,...
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
Le mardi 15 novembre 2011 =C3=A0 23:15 +0000, RenatoL a =C3=A9crit :=20 =20 =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
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
RenatoL wrote: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
On Wednesday, November 16, 2011 08:33:04 Dejan Lekic wrote:RenatoL wrote: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 - Jonathan M Davisobject[] 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
Ok, tk u all. I guess this is a very poor approach if we are looking for performance
Nov 17 2011
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 performanceMixing 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