Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 702
jogojapan Avatar asked Jan 14 '23 12:01

jogojapan


1 Answers

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)

like image 170
ratchet freak Avatar answered Feb 06 '23 23:02

ratchet freak