jastrachan | 12 Apr 13:43 2005
Picon

Re: [groovy-dev] 0..<0 not empty

On 12 Apr 2005, at 12:21, Martin C. Martin wrote:
> jastrachan@... wrote:
>> So how about if we made the following changes...
>> * ranges start with a lower value and finish with the higher value.  
>> So 0..-1 and 0..<-1 is an empty range - avoiding those nasty 
>> surprises folks have mentioned
>
> That sounds great!
>
>> * to perform reverse iteration, we use a reverse() method
>
> ok
>
>> * we support stepping of ranges via Range.step(value) which could 
>> create a new range implementation (StepRange) which adds a step value 
>> to the fromValue until its greater or equal to the toValue.
>
> Sounds great.  This works now, right?

We currently only support integer based stepping right now.

>> On a side note I confess to liking the use of negative indexes to 
>> count from the end of a collection/array/string. e.g. to get the last 
>> element of a list/array its
>>     list[-1]
>>     array[-1]
>> This won't conflict with the range changes above since negative 
>> numbers mean nothing to a range.
>
> What about the "size" notation that John and I were discussing?
>
> list[size-1]
> array[size-1]
>
> That would allow the use in ranges, and make the semantics of both 
> ranges and indexing obvious to newbies because it's so natural.

I'm wondering if the use case is large enough to warrant a new magic 
variable like 'size'? Negative indexes when using the subscript 
operator seem like such a simpler way of adding this feature to the 
language, without changing the language itself.

James
-------
http://radio.weblogs.com/0112098/


Gmane