Bug 19525 – Duration is not usable with zero-arg std.algorithm.sum

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2018-12-30T17:32:11Z
Last change time
2020-05-29T01:15:39Z
Keywords
bootcamp, pull
Assigned to
No Owner
Creator
Simen Kjaeraas

Comments

Comment #0 by simen.kjaras — 2018-12-30T17:32:11Z
Source: https://stackoverflow.com/questions/53967917/using-std-algorithm-iteration-sum-with-duration import std.algorithm : sum; import std.datetime : Duration, minutes; unittest { // Error: struct `core.time.Duration` member this is not accessible assert([1.minutes].sum() == 1.minutes); } The above code fails because sum() tries to construct Duration(0), which fails to compile. The error message (member this is not accessible) leaves something to be desired. Workaround for now: [1.minutes].sum(Duration.zero). Suggested fix: have sum() check if Seed.zero exists, and use that.
Comment #1 by dlang-bot — 2019-12-30T13:15:43Z
@Zevenberge created dlang/phobos pull request #7339 "Improved error message when calling sum without a default value" mentioning this issue: - Improved error message when calling sum without a default value If sum() is called, it tries to find a default value by constructing it with 0 (e.g. float(0)). This works for numbers, but produces obscure errors if the value cannot be constructed this way (e.g. issue #19525). The error message now states that the problem lies in the default value and suggests to supply one instead. https://github.com/dlang/phobos/pull/7339
Comment #2 by dlang-bot — 2020-01-02T09:59:20Z
dlang/phobos pull request #7339 "Improved error message when calling sum without a default value" was merged into master: - d93e7be91db04889bc5141ccb158c54f77fd2088 by Marco de Wild: Improved error message when calling sum without a default value If sum() is called, it tries to find a default value by constructing it with 0 (e.g. float(0)). This works for numbers, but produces obscure errors if the value cannot be constructed this way (e.g. issue #19525). The error message now states that the problem lies in the default value and suggests to supply one instead. https://github.com/dlang/phobos/pull/7339
Comment #3 by greeenify — 2020-05-28T13:09:06Z
Reopened as the issue hasn't been fixed yet. Duration should work out of the box with sum by e.g. checking whether .zero exists or specializing for the duration type.
Comment #4 by dlang-bot — 2020-05-28T22:13:23Z
@Biotronic created dlang/phobos pull request #7504 "Fix issue 19525 - Duration is not usable with zero-arg std.algorithm.sum" fixing this issue: - Fix issue 19525 https://github.com/dlang/phobos/pull/7504
Comment #5 by dlang-bot — 2020-05-29T01:15:39Z
dlang/phobos pull request #7504 "Fix issue 19525 - Duration is not usable with zero-arg std.algorithm.sum" was merged into master: - fd59b6a92e8c2a9e56db0c788f6b40f7d930aef0 by Biotronic: Fix issue 19525 https://github.com/dlang/phobos/pull/7504