Associative Array c'tor

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jul 11 07:00:49 PDT 2016


On 7/11/16 9:48 AM, Bahman Movaqar wrote:
> I'm processing a list of structs (MySt) with `reduce` to produce an
> associate array of type `MySt[][string]`.
> Right now I'm using the following (slimmed down) code:
>
>     MySt[][string] result;
>     return reduce!(
>       function MySt[][string](MySt[][string] acc, MySt val) {
>         // do something with acc
>         return acc;
>       }
>     )(result, inputList);
>
> I was wondering if I could remove the empty declaration line (line 1);
> for example:
>
>     return reduce!(
>       function MySt[][string](MySt[][string] acc, MySt val) {
>         // do something with acc
>         return acc;
>       }
>     )(new MySt[][string](), inputList);
>
> Obviously, this fails with the error message: "cannot pass type string
> as a function argument".
>
> I very much would like to avoid empty declaration lines like that of
> `result`.  Is there anyway you folks would suggest?

Untested, but you could try MySt[][string].init.

But passing empty AA by value sometimes can be surprising. I'm not sure 
if it will work.

-Steve




More information about the Digitalmars-d-learn mailing list