Associative array on the heap
Meta via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon May 18 17:00:29 PDT 2015
On Monday, 18 May 2015 at 23:55:40 UTC, Freddy wrote:
> How do you allocate an associative array on the heap?
> ----
> void main(){
> alias A=int[string];
> auto b=new A;
> }
> ----
> $ rdmd test
> test.d(4): Error: new can only create structs, dynamic arrays
> or class objects, not int[string]'s
> Failed: ["dmd", "-v", "-o-", "test.d", "-I."]
They are allocated on the heap implicitly; there's no need for
`new`. You actually *can't* use new with an AA, which is what the
compiler is telling you.
void main()
{
alias A = int[string];
A b = []; //No allocation yet, b is null
b["test"] = 1; //b is now non-null
}
More information about the Digitalmars-d-learn
mailing list