Comparison of TickDuration and StopWatch.peek

Jonathan M Davis jmdavisProg at gmx.com
Tue Feb 28 10:22:34 PST 2012


On Tuesday, February 28, 2012 19:15:01 Matej Nanut wrote:
> Hello everyone,
> 
> I have the following code snippet:
> 
> 
> import core.time: TickDuration;
> import std.datetime: StopWatch, AutoStart;
> 
> 
> void main()
> {
> auto wait = TickDuration.from!`msecs`(1000);
> auto timer = StopWatch(AutoStart.yes);
> 
> while (timer.peek < wait) // Okay.
> { }
> 
> while (wait >= timer.peek) // Compile error.
> { }
> }
> 
> 
> I would like to know why the second ‘while’ doesn't compile. The error is:
> 
> comparison.d(13): Error: function core.time.TickDuration.opCmp (ref
> const(TickDuration) rhs) const is not callable using argument types
> (TickDuration)
> comparison.d(13): Error: timer.peek() is not an lvalue
> 
> which I don't really understand. There shouldn't be a difference in
> ‘<’ and ‘>=’ apart from their result, or should there be? =/ Same
> thing happens for ‘>’.
> 
> Also, I can use ‘peek’ without parenthesis even though it isn't marked
> as @property on dlang.org, am I missing something? I probably read the
> spec incorrectly somewhere for this one.

It looks like TickDuration only has an overload for opCmp which takes a const 
ref, and a const ref must be an lvalue (unlike in C++, which allows const& to 
take a temporary). The reason that the first one works is that the right-hand 
side is a variable.

Create a bug report for it ( http://d.puremagic.com/issues ), and it should be 
fixed for the next release. In the meantime, you'll have to assign the right-
hand side to a variable and use that in the condition rather than using the 
return value directly.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list