Thread overview
Generic array
Nov 15, 2011
RenatoL
Nov 15, 2011
bioinfornatics
Nov 16, 2011
Jesse Phillips
Nov 16, 2011
Dejan Lekic
Nov 16, 2011
Jonathan M Davis
Nov 17, 2011
RenatoL
Nov 17, 2011
Jonathan M Davis
November 15, 2011
##[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....
November 15, 2011
Le mardi 15 novembre 2011 à 23:15 +0000, RenatoL a écrit :
> ##[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....

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 );
    }
}
--------------------------------------------------
Output:
--------------------------------------------------
type: uint, value: 1
type: immutable(char)[], value: hi
type: int, value: -2
type: char, value: 5
--------------------------------------------------




November 16, 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)...
November 16, 2011
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);
November 16, 2011
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
November 17, 2011
Ok, tk u all.
I guess this is a very poor approach if we are looking for
performance
November 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
> 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