www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Array slicing syntax

reply "Glen Perkins" <please.dont email.com> writes:
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.
Nov 14 2004
next sibling parent Derek Parnell <derek psych.ward> writes:
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
Nov 14 2004
prev sibling parent reply h3r3tic <foo bar.baz> writes:
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.
Nov 14 2004
parent "Glen Perkins" <please.dont email.com> writes:
"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.
Nov 14 2004