www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Generic container

reply lurker <lurker mailinator.com> writes:
Hello, first time visiting the newsgroup.

I see in the manual that you can create built-in dictionaries like so:

int[string] foo;

Do you know of a way to store any value in a dictionary? In C# I can do
something like this:

Dictionary<string, object> foo;

I need a dictionary that can store any value, indexed by string.

Thanks
May 08 2010
parent reply wrzosk <dprogr gmail.com> writes:
On 08.05.2010 23:52, lurker wrote:
 Hello, first time visiting the newsgroup.

 I see in the manual that you can create built-in dictionaries like so:

 int[string] foo;

 Do you know of a way to store any value in a dictionary? In C# I can do
 something like this:

 Dictionary<string, object>  foo;

 I need a dictionary that can store any value, indexed by string.

 Thanks

foo["aaa"] = new AAA();
May 08 2010
parent bearophile <bearophileHUGS lycos.com> writes:
wrzosk:
 Object[string] foo;
 foo["aaa"] = new AAA();

In D there is no the automatic boxing present in C#, so that can contain any object, but not any value. If you want to store any value, you need the values of that associative array to be of some kind of Box or Variant, etc. There are variant-like types both in Phobos and Tango, but you have to test if they work well for your purposes because it seems it's easy to put bugs in a Variant implementation. Bye, bearophile
May 08 2010