From 1caa1fc2c98c0fe06b8440f435cbac744fe7729c Mon Sep 17 00:00:00 2001 From: Steven Schveighoffer Date: Thu, 16 Mar 2017 18:13:42 -0400 Subject: [PATCH 1/2] Fix issue 8411 - add opCast!bool support for Duration. --- src/core/time.d | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/core/time.d b/src/core/time.d index 12796ed600..db8c5ad3cb 100644 --- a/src/core/time.d +++ b/src/core/time.d @@ -1104,6 +1104,21 @@ public: } } + /++ + Returns `true` if this duration is non-zero. + +/ + bool opCast(T : bool)() const nothrow @nogc + { + return _hnsecs != 0; + } + + unittest + { + auto d = 10.minutes; + assert(d); + assert(!(d - d)); + assert(d + d); + } //Temporary hack until bug http://d.puremagic.com/issues/show_bug.cgi?id=5747 is fixed. Duration opCast(T)() const nothrow @nogc From 33b41c14e25f874111691f1f7a4708c8cbce4b7d Mon Sep 17 00:00:00 2001 From: Steven Schveighoffer Date: Fri, 17 Mar 2017 09:17:33 -0400 Subject: [PATCH 2/2] Fix docs --- src/core/time.d | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/core/time.d b/src/core/time.d index db8c5ad3cb..6cd1c65e04 100644 --- a/src/core/time.d +++ b/src/core/time.d @@ -1105,7 +1105,8 @@ public: } /++ - Returns `true` if this duration is non-zero. + Allow Duration to be used as a boolean. + Returns: `true` if this duration is non-zero. +/ bool opCast(T : bool)() const nothrow @nogc {