array length vs $

Bill Baxter dnewsgroup at billbaxter.com
Sat Mar 15 09:46:47 PDT 2008


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

For what it's worth, Andrei agrees with you.  It's one of the few points 
he made before he gave up on us cretins here in the newsgroups.  His 
argument was exactly the same.  It's a stealth keyword.

So there is a chance it will happen in D2.0x.

--bb



More information about the Digitalmars-d mailing list