array length vs $

Pedro Ferreira ask at me.pt
Tue Mar 18 07:48:54 PDT 2008


Frank Benoit escreveu:
> 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 '$'.

You have my vote as well. Besides, writing array.$ is much quicker than
   array.length. Also, $ by in other places has no meaning other than
length (and tokens such as & and * have), which may make code clearer.
In my case, length is a common typo, as I am not english and frequently
end up writing 'lenght' instead.



More information about the Digitalmars-d mailing list