Removal of implicit variable "length"

Bradley Smith digitalmars-com at baysmith.com
Tue Jun 6 14:53:28 PDT 2006


In a posting from about a year ago 
(http://www.digitalmars.com/d/archives/digitalmars/D/learn/842.html), 
the implicit variable "length" was scheduled for potential
deprecation.

Is it going to be removed? If so, when?

I see this as a significant flaw in the D language. For example, what 
does the following code do?

   import std.stdio;  // for writefln

   void main() {
     static int[4] foo = [ 0, 1, 2, 3 ];
     int[] bar;
     int length = 2;
     bar = foo[0 .. length];
     writefln("bar = %s", bar);
   }


What is does the program print?
   A. bar = [0,1,2,3]
   B. bar = [0,1]

I would guess that most programmers unfamilar with D would answer "B". 
However, "A" is the correct answer. It certainly has been a lesson I've 
had to learn the hard way.

Besides, is "foo[0 .. length]" really better than "foo[0 .. 
foo.length]"? The first is slightly more convenient but the second is 
more explicit and readable.

Thanks,
   Bradley



More information about the Digitalmars-d-learn mailing list