Thread overview | |||||
---|---|---|---|---|---|
|
May 08, 2010 Generic container | ||||
---|---|---|---|---|
| ||||
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 Re: Generic container | ||||
---|---|---|---|---|
| ||||
Posted in reply to lurker | 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
Object[string] foo;
foo["aaa"] = new AAA();
|
May 09, 2010 Re: Generic container | ||||
---|---|---|---|---|
| ||||
Posted in reply to wrzosk | 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
|
Copyright © 1999-2021 by the D Language Foundation