Thread overview
SysTime.add!"days" missing
Mar 17, 2014
Spacen Jasset
Mar 17, 2014
Vladimir Panteleev
Mar 17, 2014
Spacen Jasset
Mar 17, 2014
Adam D. Ruppe
Mar 17, 2014
Spacen Jasset
Mar 17, 2014
Ary Borenszweig
Mar 17, 2014
Spacen Jasset
Mar 17, 2014
Adam D. Ruppe
Mar 17, 2014
Spacen Jasset
Mar 17, 2014
Ali Çehreli
March 17, 2014
I would like to subtract 60 days from a SysTime, but find that SysTime.add!"days" is not available, unlike Systime.roll!"days" which is available.

main.d(18): Error: template instance add!("days") add!("days") does not match te
mplate declaration add(string units)(long value, AllowDayOverflow allowOverflow
= AllowDayOverflow.yes) if (units == "years" || units == "months")



Why is "days" add missing? How might I get round this?

Regards,

Spacen.
March 17, 2014
On Monday, 17 March 2014 at 11:11:26 UTC, Spacen Jasset wrote:
> I would like to subtract 60 days from a SysTime, but find that SysTime.add!"days" is not available, unlike Systime.roll!"days" which is available.

You can do this in a simpler way: t -= 60.days;

SysTime.add likely specializes on durations the length of which varies depending from their starting point, due to the varying number of days in a month or in a year.
March 17, 2014
On Monday, 17 March 2014 at 11:52:08 UTC, Vladimir Panteleev wrote:
> On Monday, 17 March 2014 at 11:11:26 UTC, Spacen Jasset wrote:
>> I would like to subtract 60 days from a SysTime, but find that SysTime.add!"days" is not available, unlike Systime.roll!"days" which is available.
>
> You can do this in a simpler way: t -= 60.days;
>
> SysTime.add likely specializes on durations the length of which varies depending from their starting point, due to the varying number of days in a month or in a year.

Thanks. What devilish magic allows for the syntax 60.days? (how does it work)


March 17, 2014
On Monday, 17 March 2014 at 14:31:54 UTC, Spacen Jasset wrote:
> Thanks. What devilish magic allows for the syntax 60.days? (how does it work)

There's a function in core.time:

Duration days(int n);


D functions f(x, t...) can also be called x.f(t) (or x.f without parameters if there's no additional arguments).

This works on all types, it is called uniform function call syntax, or UFCS. It lets us extend other things with new methods.
March 17, 2014
On Monday, 17 March 2014 at 14:39:16 UTC, Adam D. Ruppe wrote:
> On Monday, 17 March 2014 at 14:31:54 UTC, Spacen Jasset wrote:
>> Thanks. What devilish magic allows for the syntax 60.days? (how does it work)
>
> There's a function in core.time:
>
> Duration days(int n);
>
>
> D functions f(x, t...) can also be called x.f(t) (or x.f without parameters if there's no additional arguments).
>
> This works on all types, it is called uniform function call syntax, or UFCS. It lets us extend other things with new methods.


Thanks Adam, is there a good explanation anywhere? It must be newish.
March 17, 2014
On 3/17/14, 12:11 PM, Spacen Jasset wrote:
> On Monday, 17 March 2014 at 14:39:16 UTC, Adam D. Ruppe wrote:
>> On Monday, 17 March 2014 at 14:31:54 UTC, Spacen Jasset wrote:
>>> Thanks. What devilish magic allows for the syntax 60.days? (how does
>>> it work)
>>
>> There's a function in core.time:
>>
>> Duration days(int n);
>>
>>
>> D functions f(x, t...) can also be called x.f(t) (or x.f without
>> parameters if there's no additional arguments).
>>
>> This works on all types, it is called uniform function call syntax, or
>> UFCS. It lets us extend other things with new methods.
>
>
> Thanks Adam, is there a good explanation anywhere? It must be newish.

http://dlang.org/function.html#pseudo-member
March 17, 2014
On Monday, 17 March 2014 at 15:24:22 UTC, Ary Borenszweig wrote:
> On 3/17/14, 12:11 PM, Spacen Jasset wrote:
>> On Monday, 17 March 2014 at 14:39:16 UTC, Adam D. Ruppe wrote:
>>> On Monday, 17 March 2014 at 14:31:54 UTC, Spacen Jasset wrote:
>>>> Thanks. What devilish magic allows for the syntax 60.days? (how does
>>>> it work)
>>>
>>> There's a function in core.time:
>>>
>>> Duration days(int n);
>>>
>>>
>>> D functions f(x, t...) can also be called x.f(t) (or x.f without
>>> parameters if there's no additional arguments).
>>>
>>> This works on all types, it is called uniform function call syntax, or
>>> UFCS. It lets us extend other things with new methods.
>>
>>
>> Thanks Adam, is there a good explanation anywhere? It must be newish.
>
> http://dlang.org/function.html#pseudo-member

Thanks but I still can't see how it fully explains this: writeln("60 days: ", 60.days);

There is some type magic going on somewhere, because 60 is an int and days is a random method that happens to belong to Duration.

March 17, 2014
On Monday, 17 March 2014 at 16:16:20 UTC, Spacen Jasset wrote:
> int and days is a random method that happens to belong to Duration.

There's a separate function days

https://github.com/D-Programming-Language/druntime/blob/master/src/core/time.d#L928

that one belongs to Duration

https://github.com/D-Programming-Language/druntime/blob/master/src/core/time.d#L1414

this one doesn't.


In the 60.days instance, it is calling the second function.
March 17, 2014
On Monday, 17 March 2014 at 16:33:34 UTC, Adam D. Ruppe wrote:
> On Monday, 17 March 2014 at 16:16:20 UTC, Spacen Jasset wrote:
>> int and days is a random method that happens to belong to Duration.
>
> There's a separate function days
>
> https://github.com/D-Programming-Language/druntime/blob/master/src/core/time.d#L928
>
> that one belongs to Duration
>
> https://github.com/D-Programming-Language/druntime/blob/master/src/core/time.d#L1414
>
> this one doesn't.
>
>
> In the 60.days instance, it is calling the second function.

Thanks Adam, I see how the magic happens now.
March 17, 2014
On 03/17/2014 08:11 AM, Spacen Jasset wrote:

> Thanks Adam, is there a good explanation anywhere?

Here is another one:

  http://ddili.org/ders/d.en/ufcs.html

Ali