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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With