www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: (git HEAD) std.datetime spewing deprecation messages

reply Jonathan M Davis via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Thu, 5 Jun 2014 14:35:16 +0000 (UTC)
Byron Heads via Digitalmars-d <digitalmars-d puremagic.com> wrote:

 On Thu, 05 Jun 2014 07:18:59 -0700, H. S. Teoh via Digitalmars-d
 wrote:

 On Thu, Jun 05, 2014 at 01:23:47AM -0700, Jonathan M Davis via
 Digitalmars-d wrote:
 {
     auto d = dur!"days"(12) + dur!"minutes"(7) +
 dur!"usecs"(501223); long days;
     int seconds;
     short msecs;
     d.split!("days", "seconds", "msecs")(&days, &seconds, &msecs);
     assert(days == 12);
     assert(seconds == 7 * 60);
     assert(msecs == 501);

Very nice! I like this. It looks very D-ish, and very idiomatic.

if only we could avoid the quotes, would a enum array work? d.split![days, second, msecs](...)

It's a common idiom in core.time and std.datetime to use strings to represent units when you need to give the units as template arguments. If it hade't been strings, it would have been an enum (otherwise, they would risk conflicting with local variables and whatnot), in which case it would have been even more verbose - e.g. Units.days, Unit.seconds, Unit.msecs (and IIRC, I originally had something like that until Anrei suggested that I use strings in the original review for std.datetime). Also, days, seconds, and msecs are free functions in core.time which forward to dur!"days", dur!"seconds", and dur!"msecs", so trying to use them on their own would try and use those free functions, which obviously isn't what we want at all. I can see why you might want to remove the quotes, but they really aren't that bad, and using strings for this purpose has turned out to be extremely useful and flexible. - Jonathn M Davis
Jun 05 2014
parent Byron Heads <byron.heads gmail.com> writes:
On Thu, 05 Jun 2014 21:00:39 +0200, Jonathan M Davis via Digitalmars-d
wrote:

 It's a common idiom in core.time and std.datetime to use strings to
 represent units when you need to give the units as template arguments.
 If it hade't been strings, it would have been an enum (otherwise, they
 would risk conflicting with local variables and whatnot), in which case
 it would have been even more verbose - e.g. Units.days, Unit.seconds,
 Unit.msecs (and IIRC, I originally had something like that until Anrei
 suggested that I use strings in the original review for std.datetime).
 Also, days, seconds, and msecs are free functions in core.time which
 forward to dur!"days", dur!"seconds", and dur!"msecs", so trying to use
 them on their own would try and use those free functions, which
 obviously isn't what we want at all. I can see why you might want to
 remove the quotes, but they really aren't that bad, and using strings
 for this purpose has turned out to be extremely useful and flexible.
 
 - Jonathn M Davis

Yeah its more of a pipe dream, kinda of how we have moved away from passing strings and use the short hand versions of lambdas. A CTFE helper might make it look better, saw this somewhere on the form. d.splitHelper!q{days, second, msecs}(...) the function name is not import here just the idea. There is a stdx project on code.dlang, might be a good place for little helpers like this.
Jun 06 2014