Negative array indices

Douglas Crockford crock@communities.com
Thu, 15 Oct 1998 10:36:36 -0700


>>It is true that allowing negative indices may mask some bugs -- however,
>>it's a question of weighing how often that is likely to occur against
>>how often it will be convenient to be able to index from the back.


>To achieve what Ping likes in Java you do:
>
>   Object[] foo = blah...
>   Object fromBack = foo[foo.length - index];
>
>Now we could talk about syntactic sugar which would make this more
>palatable if foo were some complex, side-effect causing expression, but I
>don't do syntax.


Negative indices are desirable in a scripting language and undesirable in an
application or system language. There may be some syntactic rat poison that
works for both, such as

   Object fromBack := foo[$ index]

where '[$' expands into '[foo.length -' . This is stuff that might better be
handled by the text editor in the console window than by the language.