D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7606 - core.time.TickDuration opCmp accepts only lvalues
Summary: core.time.TickDuration opCmp accepts only lvalues
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: druntime (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-02-28 13:11 UTC by Matej Nanut
Modified: 2012-03-14 00:38 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Matej Nanut 2012-02-28 13:11:53 UTC
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.
     { }
  }

Compiler output:

  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

I assume the same thing happens elsewhere in the library. As D strives to be an intuitive language, I reason this to be a bug. "<" and ">=" should work identically in this circumstance.
Comment 1 Alex Rønne Petersen 2012-02-28 13:13:53 UTC
What's happening here is that TickDuration's opCmp takes a 'ref' value. This is exactly the reason I *always* avoid ref on op*(), because it creates annoying situations like this one (in fact, I ran into this today as well).

I don't know if auto ref would fix this.
Comment 2 Jonathan M Davis 2012-02-28 14:13:21 UTC
There's no really no reason for opCmp to take anything other than a straight TickDuration in this case. TickDuration holds 1 long and that's it. It's not particularly large or complicated.

In the general case, using auto ref would probably be the correct solution, but auto ref doesn't currently work for non-templated functions, which opCmp obviously isn't.
Comment 3 github-bugzilla 2012-03-14 00:26:42 UTC
Commit pushed to master at https://github.com/D-Programming-Language/druntime

https://github.com/D-Programming-Language/druntime/commit/20fc5643c5f7111feb2caa55c8d86d1a50ea7f0e
Merge pull request #172 from jmdavis/time

Fix Issue 7606