First class lazy Interval

Denis Koroskin 2korden at gmail.com
Fri Feb 27 05:53:28 PST 2009


On Fri, 27 Feb 2009 16:44:31 +0300, Andrei Alexandrescu <SeeWebsiteForEmail at erdani.org> wrote:

> Michel Fortin wrote:
>> On 2009-02-27 04:43:46 -0500, bearophile <bearophileHUGS at lycos.com>  
>> said:
>>
>>> D2 supports the interval syntax in the foreach:
>>> foreach (i; 1..1000) {...}
>>>
>>> Such intervals are useful in a very large number of situations. So,  
>>> with the new Range support, it may be useful to allow the interval  
>>> syntax to be used in other contexts as well.
>>> So x..y may become a first-class lazy interval from x to y-1, that can  
>>> be passed to functions too, etc, and not just used into foreach (the  
>>> compiler can recognize it, and often optimize it away in many  
>>> situations, replacing it with a normal for() loop).
>>  I agree that having first-class intervals in the language would make  
>> it better, especially when you want to pass intervals as function  
>> arguments.
>
> I'm having trouble understanding what's wrong with the good old data  
> types and functions.
>
> Andrei

The syntax. One may want to reuse 0..100 syntax to generate random number:
auto x = random(0..100); // gimme a random value in [0, 100)

or check if a value belongs to an interval:

T opIndex(size_t index)
{
    assert(index in 0.._size);
    // ...
}




More information about the Digitalmars-d mailing list