Create a general array

2014-07-24 Thread Robert Rimoczi via Digitalmars-d-learn

Hi!

Can I somehow make a general array where I can store everykind of 
object? doubles, ints, float, class objects? Or is there any 
method for it?


Re: Create a general array

2014-07-24 Thread Brad Anderson via Digitalmars-d-learn

On Thursday, 24 July 2014 at 19:43:23 UTC, Robert Rimoczi wrote:

Hi!

Can I somehow make a general array where I can store everykind 
of object? doubles, ints, float, class objects? Or is there any 
method for it?


void main()
{
import std.variant, std.stdio;
class Four { override string toString() const { return "4"; } }


Variant[] anything;

anything ~= Variant(1.0);
anything ~= Variant(2);
anything ~= Variant(3.0f);
anything ~= Variant(new Four());

writeln(anything);
}

You'll want to read up on std.variant.Variant to understand how
to use it. Look at get() in particular but there are other
methods that you may need depending on how you are using it.