On Wed, 04 Apr 2012 12:06:33 +0200, Jacob Carlborg <d...@me.com> wrote:
On 2012-04-04 04:11, Jonathan M Davis wrote:
foreach(i; 0 .. 5)
is more efficient only because it has _nothing_ to do with arrays.
Generalizing
the syntax wouldn't help at all, and if it were generalized, it would
arguably
have to be consistent in all of its uses, in which case
foreach(i; 0 .. 5)
would become identical to
foreach(i; [0, 1, 2, 3, 4])
and therefore less efficient. Generalizing .. just doesn't make sense.
Why couldn't the .. syntax be syntax sugar for some kind of library
implement range type, just as what is done with associative arrays.
We could implement a new library type, named "range". Looking something
like this:
struct range
{
size_t start;
size_t end;
// implement the range interface or opApply
}
range r = 1 .. 5;
The above line would be syntax sugar for:
range r = range(1, 5);
void foo (range r)
{
foreach (e ; r) {}
}
foo(r);
This could then be taken advantage of in other parts of the language:
class A
{
int opSlice (range r); // new syntax
int opSlice (size_t start, size_t end); // old syntax
}
I think this would be completely backwards compatible as well.
And what do we do with 3..$?