Variants array and IDK

Nick Sabalausky a at a.a
Mon Jan 31 01:40:15 PST 2011


"g g" <g at g.com> wrote in message news:ii5mtb$12qv$1 at digitalmars.com...
> IDK where to put this.
>
> first thing:
> Could it be a way to collapse Variants in std.variant?, like
> Variant x = Variant(1)
> Variant y = Variant([x,x,x,x])
> //y could be simplified to Variant(int[]) instead of 
> Variant(Variant(int)[])
> //now it is not implemented (as far i know)
>

The problem with that is that x is only an int at runtime. It's the same 
problem as this:

    Variant x = Variant(1);
    int z = x;  // ERROR: x might not be an int.

You have to do it like this:

    Variant x = Variant(1);
    if(x.convertsTo!int())
    {
        int z = x.coerce!int();
    }
    else
       // Do something else

Or you skip the "if(x.convertsTo!int())" part if you expect that x should 
always be an int. If it ever isn't an int then coerce will throw an 
exception.

So if you want y to be Variant(int[]), you need to do:

    Variant x = Variant(1);
    if(x.convertsTo!int())
    {
        auto xInt = x.coerce!int();
        Variant y = Variant([xInt,xInt,xInt,xInt]);
    }
    else
       // Do something else





More information about the Digitalmars-d mailing list