Weird interaction of design choices: Duration + sum

Luís Marques luis at luismarques.eu
Fri Oct 13 18:36:58 UTC 2017


If you do this:

     import std.algorithm : sum;
     import core.time : Duration;

     Duration[] parts;
     auto total = parts.sum;

You'll get an error ("struct core.time.Duration member this is 
not accessible"). That's because the Duration constructor is 
private, and `sum` calls `sum(r, Unqual!Seed(0))`. BTW, not the 
most helpful of messages for beginners, maybe this could be 
phrased to say the ctor is private?

I suppose that the ctor is private due to the arbitrary nature of 
its input units (hecto-nanoseconds), even though Duration(0) is 
unambiguous. On the other hand, sum has to use Seed(0) instead of 
Seed.init because some types don't use 0 as .init, namely floats. 
The result is that you have to use range.sum(Duration.zero), or 
Duration.init.

Something here rubs me the wrong way, but I can't tell exactly 
what it is, or what can be done about it :)


More information about the Digitalmars-d mailing list