Questions about the slice operator

Timon Gehr timon.gehr at gmx.ch
Thu Apr 5 09:29:07 PDT 2012


On 04/04/2012 12:06 PM, Jacob Carlborg 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.
>
> ...
>
> I think this would be completely backwards compatible as well.
>

It would be awkward to introduce it in a backwards compatible way, 
because currently '..' binds weaker than any operator.

auto x = 0..10; // ok
auto y = 0..10, z = 2; // error, z not defined
x = 0..11; // error: expression '11' has no effect


More information about the Digitalmars-d-learn mailing list