jogojapan
jogojapan

Reputation: 70007

D language: Return freshly created associative array

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

One way of doing it is this:

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

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

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

or,

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

or,

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

None of these work, for reasons related to how associative arrays need to be declared. Is there a way?

Upvotes: 4

Views: 600

Answers (1)

ratchet freak
ratchet freak

Reputation: 48216

you can use

return (int[char]).init;

so you don't have to declare it.

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

Upvotes: 9

Related Questions