Generic array

bioinfornatics bioinfornatics at fedoraproject.rog
Tue Nov 15 15:30:44 PST 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
--------------------------------------------------






More information about the Digitalmars-d-learn mailing list