digitalmars.D.learn - Duration at runtime
- Zekereth (14/14) Feb 18 2016 I'm confused by the following:
- Adam D. Ruppe (6/8) Feb 18 2016 "seconds" is a literal value that the compiler knows about.
- Zekereth (4/12) Feb 18 2016 Thanks a lot Adam!
- Zekereth (4/18) Feb 18 2016 Never mind I found a better solution to my problem by storing a
- Steven Schveighoffer (7/24) Feb 19 2016 Because it might help with some future issue:
I'm confused by the following: import std.stdio; import std.datetime; void main() { string unitType = "seconds"; auto seconds = 1; // auto myDur = dur!(unitType)(seconds); // Error unitType can't be read at compile time. auto myDur = dur!("seconds")(seconds); // Compiles why? } How is seconds able to be read at compile time but unitType cannot? Thanks!
Feb 18 2016
On Friday, 19 February 2016 at 04:08:02 UTC, Zekereth wrote:How is seconds able to be read at compile time but unitType cannot?"seconds" is a literal value that the compiler knows about. unitType is a variable that might change between its declaration and use (it doesn't here, but the compiler doesn't check if it actually does, just if it *can*), so the compiler doesn't allow it.
Feb 18 2016
On Friday, 19 February 2016 at 04:16:23 UTC, Adam D. Ruppe wrote:On Friday, 19 February 2016 at 04:08:02 UTC, Zekereth wrote:Thanks a lot Adam! So is there a way around this?. I want duration to be configurable at runtime.How is seconds able to be read at compile time but unitType cannot?"seconds" is a literal value that the compiler knows about. unitType is a variable that might change between its declaration and use (it doesn't here, but the compiler doesn't check if it actually does, just if it *can*), so the compiler doesn't allow it.
Feb 18 2016
On Friday, 19 February 2016 at 04:21:43 UTC, Zekereth wrote:On Friday, 19 February 2016 at 04:16:23 UTC, Adam D. Ruppe wrote:Never mind I found a better solution to my problem by storing a Duration instead of the unitType. Works just fine. Thanks a lot I appreciate your help!On Friday, 19 February 2016 at 04:08:02 UTC, Zekereth wrote:Thanks a lot Adam! So is there a way around this?. I want duration to be configurable at runtime.How is seconds able to be read at compile time but unitType cannot?"seconds" is a literal value that the compiler knows about. unitType is a variable that might change between its declaration and use (it doesn't here, but the compiler doesn't check if it actually does, just if it *can*), so the compiler doesn't allow it.
Feb 18 2016
On 2/18/16 11:36 PM, Zekereth wrote:On Friday, 19 February 2016 at 04:21:43 UTC, Zekereth wrote:Because it might help with some future issue: Instead of auto, declare the unitType as immutable or enum: immutable unitType = "seconds"; enum unitType = "seconds"; Then the compiler knows it won't change. -SteveOn Friday, 19 February 2016 at 04:16:23 UTC, Adam D. Ruppe wrote:Never mind I found a better solution to my problem by storing a Duration instead of the unitType. Works just fine. Thanks a lot I appreciate your help!On Friday, 19 February 2016 at 04:08:02 UTC, Zekereth wrote:Thanks a lot Adam! So is there a way around this?. I want duration to be configurable at runtime.How is seconds able to be read at compile time but unitType cannot?"seconds" is a literal value that the compiler knows about. unitType is a variable that might change between its declaration and use (it doesn't here, but the compiler doesn't check if it actually does, just if it *can*), so the compiler doesn't allow it.
Feb 19 2016