Bug 8411 – core.time: No easy way to check if Duration is empty
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
druntime
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-07-22T06:46:00Z
Last change time
2017-08-07T12:25:48Z
Keywords
bootcamp, pull
Assigned to
nobody
Creator
dlang-bugzilla
Comments
Comment #0 by dlang-bugzilla — 2012-07-22T06:46:38Z
The simplest way to check for an empty Duration seems to be:
if (!duration.total!"hnsecs"()) { ... }
I suggest either implementing an isEmpty property (given that there already is an isNegative property), or implement opCast!bool so that the following will work:
if (!duration) { ... }
Comment #1 by issues.dlang — 2013-01-24T12:36:20Z
We now have Duration.zero, so
if(duration != Duration.zero)
will work as will
if(duration != Duration.init)
But there's no such thing as an "empty" duration. All durations have a value, and they aren't ranges or containers. It makes as much sense to have an isEmpty property on a Duration as it does to have it on an int.
Comment #2 by dlang-bugzilla — 2013-01-24T23:13:59Z
An interval has a .empty property, though.
Comment #3 by schveiguy — 2017-03-16T22:17:54Z
I have a further, more difficult use case for this:
if(auto d = calculateSomeDuration())
{
display(d);
}
This is not as easily worked around with the current API.
Adding opCast!bool was easy enough: https://github.com/dlang/druntime/pull/1793
Comment #4 by github-bugzilla — 2017-03-17T15:00:49Z