(git HEAD) std.datetime spewing deprecation messages

Artur Skawina via Digitalmars-d digitalmars-d at puremagic.com
Thu Jun 5 14:54:49 PDT 2014


On 06/05/14 16:18, H. S. Teoh via Digitalmars-d wrote:
> On Thu, Jun 05, 2014 at 01:23:47AM -0700, Jonathan M Davis via Digitalmars-d wrote:
>> Actually, after some further discussion, I think that we've decided to
>> remove get/getOnly entirely. Instead, we'll have a function called
>> split which splits the Duration among the units that you request
>> (rather than just one).  I'll be updating the current pull request
>> with those changes shortly, so we may not end up with exactly what I
>> have at the moment, but these are the currently proposed documentation
>> examples for split:
>>
>> {
>>     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.

I'd say it is very C-ish and very unidiomatic.

Why? Well, compare with:

   struct D {
      // In real code these methods would do the work [1]:
      long days() @property { return 2012; }
      int seconds() @property { return 34; }
      short msecs() @property { return 567; }

      auto split(string FIELDS)() @property {
         static struct Result {
            mixin(FIELDS.splitter(',').map!q{`typeof(D.`~stripLeft(a)~") "~a~";\n"}().join());
         }
         return mixin(`Result(`~FIELDS~`)`);
      }
   }

   void main() {
      auto d = D();
      auto r = d.split!q{ days, seconds, msecs };
      import std.stdio;
      writeln(r, r.days, r.seconds, r.msecs);
   }

No unnecessary declarations and redundancy.

For cases where the target is already predeclared, a pseudo-destructuring
syntax can also be used:

   template destr(A...) { void destr(S)(S s) @property { A = s.tupleof; } }

   void main() {
      long days;
      int seconds;
      short msecs;
      auto d = D();
      destr!(days, seconds, msecs) = d.split!q{ days, seconds, msecs };
      import std.stdio;
      writeln(days, seconds, msecs);
   }


Note that I have never even used std.datetime [1] and don't plan to; please
ignore this when deciding its API. I'm just saying that encouraging that kind
of return-by-weakly-typed-pointers-with-string-selectors interfaces is /not/
a good idea; there are other, much better, options in D.

artur

[1] Of course in real code the `split` implementation would probably use
    an intermediate struct to hold common state that is used while calculating
    the result. Also for namespace reasons. But that's just a caller-invisible
    implementation detail.

[2] other than as input for parsing benchmarks. :)


More information about the Digitalmars-d mailing list