Bug 12837 – Duration.get and Duration's individual unit getters are bug-prone

Status
REOPENED
Severity
enhancement
Priority
P4
Component
druntime
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-06-01T20:23:22Z
Last change time
2024-12-07T13:33:55Z
Assigned to
No Owner
Creator
Jonathan M Davis
Moved to GitHub: dmd#17131 →

Comments

Comment #0 by issues.dlang — 2014-06-01T20:23:22Z
Okay. Duration currently has long total(string units)() @safe const pure nothrow {} and long get(string units)() @safe const pure nothrow {} It then has a series of wrappers around get for get!"weeks"(), get!"hours"(), etc, which bear the unit names - weeks, hours, etc. I have a similar type in C++ at work, which does not have the wrappers, and what I have found is that people consistently misuse get, thinking that it's total, and it keeps causing bugs. I don't know that that's happening with get much with core.time.Duration, because we have the individual unit wrappers, but I fully expect that folks are misusing those the some way. That being the case, I think that we need to change these functions on Duration to make this clearer and prevent such bugs, which are often hard to track down. So, I propose that we add getOnly (which is the same as get) and deprecate get (which probably won't cause many folks to change their code, since almost everyone will probably be using the wrappers) as well as deprecate all of the individual unit wrapper functions. This _does_ make code that uses them longer - e.g. d.getOnly!"hours"() instead of d.hours - but odds are that in most cases where they're used, it's a bug anyway, and they should have been using total. So, this will cause some folks to change their code, but in a lot of those cases, it will probably catch bugs for them.
Comment #1 by issues.dlang — 2014-06-01T23:45:25Z
Comment #2 by issues.dlang — 2014-06-05T09:42:10Z
Comment #3 by robert.schadek — 2024-12-07T13:33:55Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/17131 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB