Thread overview
Array slicing syntax
Nov 14, 2004
Glen Perkins
Nov 15, 2004
Derek Parnell
Nov 15, 2004
h3r3tic
Nov 15, 2004
Glen Perkins
November 14, 2004
I'm wondering why something similar to Python's array slicing syntax isn't used in D. For example, I frequently see programs starting with:

foreach (char[] arg; args[1 .. args.length])

and I wonder why that isn't just

foreach (char[] arg; args[1..])


It seems as though the compiler could pretty easily fill in the blanks as in:

arr[1..] would mean arr[1 .. arr.length]
arr[..i] would mean arr[0 .. i]
arr[-1] would mean arr[arr.length-1]
arr[-i..] would mean arr[arr.length-i .. arr.length]

and so on. It's pretty convenient in Python to just let it fill in the description of where the end (or beginning) of the array is located and to be able to count from either end of the array.

Just wondering.






November 15, 2004
On Sun, 14 Nov 2004 15:46:49 -0800, Glen Perkins wrote:

> I'm wondering why something similar to Python's array slicing syntax isn't used in D. For example, I frequently see programs starting with:
> 
> foreach (char[] arg; args[1 .. args.length])
> 
> and I wonder why that isn't just
> 
> foreach (char[] arg; args[1..])
> 
> It seems as though the compiler could pretty easily fill in the blanks as in:
> 
> arr[1..] would mean arr[1 .. arr.length]
> arr[..i] would mean arr[0 .. i]
> arr[-1] would mean arr[arr.length-1]
> arr[-i..] would mean arr[arr.length-i .. arr.length]
> 
> and so on. It's pretty convenient in Python to just let it fill in the description of where the end (or beginning) of the array is located and to be able to count from either end of the array.
> 
> Just wondering.

I guess it's mostly to avoid ambiguities.

arr[1..] could just mean that the coder made a mistake and forgot the ending index.

arr[..i] could just mean that the coder made a mistake and forgot the starting index.

arr[-1] is syntacially like arr[i], and it could be a mistake in calculating the value of 'i' giving a negative value.

Currently there is a type of shorthand to help in these situations...

arr[1..length]
arr[length]
arr[length-i..length]

The identifier's name does not have to be repeatedly typed in.

-- 
Derek
Melbourne, Australia
15/11/2004 11:08:14 AM
November 15, 2004
Glen Perkins wrote:
> arr[-1] would mean arr[arr.length-1]
> arr[-i..] would mean arr[arr.length-i .. arr.length]

as for the negative indices, i've already asked about it. it's mainly for performance reasons. you'd have to check the sign in realtime because in the compile time you won't always know.
November 15, 2004
"h3r3tic" <foo@bar.baz> wrote in message news:cn8smu$800$1@digitaldaemon.com...
> Glen Perkins wrote:
>> arr[-1] would mean arr[arr.length-1]
>> arr[-i..] would mean arr[arr.length-i .. arr.length]
>
> as for the negative indices, i've already asked about it. it's mainly for performance reasons. you'd have to check the sign in realtime because in the compile time you won't always know.

Ah, yes. For a language like D, which emphasizes performance, I agree that avoiding that runtime check is worth the extra typing for the negative index examples.