D-language: returns the newly created associative array

In the factory function, I sometimes want to do nothing but return the newly created empty associative array.

One way to do this:

auto make_dict() { int[char] dict; return dict; } 

Is there a way to avoid declaring a local dict variable? Something along the lines

 auto make_dict() { return int[char]; } 

or,

 auto make_dict() { return int[char](); } 

or,

 auto make_dict() { return new int[char]; } 

None of these works for reasons related to how associative arrays should be declared. Is there any way?

+4
source share
1 answer

you can use

 return (int[char]).init; 

therefore you do not need to declare it.

the init property for all types indicates the default initialization value for the type (null for references, an empty dynamic array, and an empty associative array with the current implementation)

+9
source

All Articles