Array indexing and slicing

Norbert Nemec Norbert at Nemec-online.de
Mon Apr 3 15:07:00 PDT 2006


Obviously we both agree that "$" is nicer than nothing (i.e. better than
having to use ".length")

I also agree with you that Python syntax looks nicer than D syntax. However

a) Python syntax is not an option for D, because it would require
runtime checks that result in an unnecessary performace penalty. This
cannot be compared to array bounds checks, which can be turned off after
debugging phase. The performance penalty would have to be payed by
everyone using arrays, even if they are not even interested in negative
indexing

b) The $ is even more powerful than Python-syntax in at least one very
interesting way:
	somearray[x%$,y%$,z%$]
is the most beautiful solution I ever saw for the common problem of
periodic boundaries!

B.t.w.: Be careful!

D syntax: somearray[1..$-1,1..$-1]
is equivalent to
Python syntax: somearray[1..-1,1..-1]

you mixed this up in your examples bringing in a -2 somewhere.


Finally you asked:
>>* omissive indexes are a nice abbreviation but have far less power than
>>the "$" symbol
> 
> Which cases are you thinking about?

I call "omissive indices" only "[.." instead of "[0.." and "..]" instead
of "..$]". Obviously these cover only the trivial corner cases and the
syntax cannot naturally be extended to anything beyond these.

Greetings,
Norbert



More information about the Digitalmars-d mailing list