sum across an array of objects

Philip Daniels Philip.Daniels1971 at gmail.com
Sat Jul 28 06:49:26 PDT 2012


On Saturday, 28 July 2012 at 10:58:45 UTC, bearophile wrote:
> Philip Daniels:
>
>> cannot implicitly convert expression (_param_1) of type 
>> WordDatabase.WordTable to int
>
> That error tells you to not use an int as first value for the 
> reduce. Maybe this is enough to solve your problem.
>
> Bye,
> bearophile

Well...sort of, but I am not sure why. I was able to get to the 
right syntax by first doing it in parts, using map! and reduce! 
on separate lines, which then gave me a pointer to the original 
problem. It seems to be something to do with whether the array is 
static or dynamic. If the array is static, which mine was, then 
this form will work - note the extra [] on _word_tables:

   auto total = reduce!"a + b.size()"(0L, _word_tables[]);

But if the array is dynamic then both of these forms work

   auto total = reduce!"a + b.size()"(0L, _word_tables);
   auto total = reduce!"a + b.size()"(0L, _word_tables[]);

That seems counter-intuitive to me at the moment, coming brand 
new to the language. I know that the [] meaans "take a slice of 
the array" but I don't see why that should be considered 
different, since it is the entire array.



More information about the Digitalmars-d-learn mailing list