Create a general array

Brad Anderson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 24 12:50:21 PDT 2014


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.


More information about the Digitalmars-d-learn mailing list