www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Create a general array

reply "Robert Rimoczi" <rimorobi gmail.com> writes:
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?
Jul 24 2014
parent "Brad Anderson" <eco gnuk.net> writes:
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.
Jul 24 2014