array length vs $
Frank Benoit
keinfarbton at googlemail.com
Sat Mar 15 07:47:43 PDT 2008
Given this array:
int[] bar = new int[4];
This is equivalent:
bar[ 1 .. length ] <-> bar[ 1 .. $ ]
bar[ length -1 ] <-> bar[ $-1 ]
'length' is implicitely declared within [ ].
Since long time i think 'length' should be removed, because it can
conflict with a local 'length' variable. We have the special symbol $,
which does not have this problem.
I think this matters in case of porting code. 'length' is an often used
variable name, especially in code portions acting with arrays. When
porting such stuff, all 'length' variables need to be renamed.
But i see no benefit from this 'length'. It seems surprising for me. I
always use $. When i see foreign code using 'length', it looks wrong to me.
Implicit 'length' does implicitely disallow the name 'length' for all
other integer like variables that are potentially used in array index
calculations. In fact, i don't use the name 'length' for any variable in
D because of this.
I wish, the implicit 'length' would be deprecated and then removed from D.
Summay:
- One thing/rule more to keep in mind
- No benefit
- It exists because it existed before we had $
- Can cause problems
- Restricts the use of the name 'length' for other variables.
Removing 'length' would not break code in unexpected ways. It would
yield compile errors in every case and can be fixed by simple replacing
'length' to '$'.
More information about the Digitalmars-d
mailing list