(git HEAD) std.datetime spewing deprecation messages

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Thu Jun 5 15:15:11 PDT 2014


On Thu, 05 Jun 2014 17:54:49 -0400, Artur Skawina via Digitalmars-d  
<digitalmars-d at puremagic.com> wrote:

> 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.

auto r = d.split!("days", "seconds", "msecs")();
writeln(r, r.days, r.seconds, r.msecs);

> 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);
>    }

This requires odd machinery. Most people aren't used to writing code this  
way, and it would be difficult to explain. It's also not much different  
than the example above, and requires the compiler to inline a whole bunch  
of stuff in order to be efficient. Simple pointers to targets works very  
well. readf and getopt work this way, and are very effective.

> 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.

Respectfully disagree, the API looks very good to me. And decidedly not  
C-like.

-Steve


More information about the Digitalmars-d mailing list