> On 1 May 2018, at 22:02, Sean McAfee <eef...@gmail.com> wrote:
> 
> Today I wanted to write an infinite sequence of Dates starting from today, 
> but I accidentally wrote two dots instead of three as I'd intended.  I was 
> surprised to find that it seems to work as one might expect, eg:
> 
>     (Date.today .. *)[7] # one week from today
> 
> The online docs say that Ranges are only for numbers and strings, so how is 
> it that this works?  Is it an accident, or can anything that has a "succ" 
> method be in a Range, or...?

Indeed, when a Range consists of something else than we normally expect from a 
range (like a number), the iterator falls back on calling the .succ method on 
the object to get the succeeding object until it smart-matches with the end 
condition.  Since the Date class provides that method, this just works.

By the way, this same logic is used for ++ and — (the latter calling the .pred 
method):

$ 6 'my $date = Date.today; say $date++ for ^5'
2018-05-02
2018-05-03
2018-05-04
2018-05-05
2018-05-06

$ 6 'my $date = Date.today; say $date-- for ^5'
2018-05-02
2018-05-01
2018-04-30
2018-04-29
2018-04-28

Takeaway from this: if you have an object that you want to be incrementable and 
usable in Ranges, simply provide .succ and .pred methods.


Getting back to maybe your original question: if you want the date 7 days from 
now, you can just use integer arithmetic:

$ 6 'say Date.today + 7'
2018-05-09

Reply via email to