January 11, 2004
Andrew Edwards wrote:

> The documentation
> (http://www.digitalmars.com/d/statement.html#block) states the
> following:
[...]
> Please update doc.

Yes. There is a contradiction to the section `Local Variables' in http://www.digitalmars.com/d/function.html, [stated 11.01.04].

In that section the equivalently declared variable `z' is explicitly
stated as legal.


Moreover, for the for-statement the doc says:
<quote>
If Initializer declares a variable, that variable's scope extends
through the end of Statement. For example:

	for (int i = 0; i < 10; i++)
		foo(i);

is equivalent to:
	{   int i;
	    for (i = 0; i < 10; i++)
		foo(i);
	}
</quote>

However the following compiles without errors:
<code>
void func5()
{
  char*[] sarr;
  char* s;
  int i;

  for(int i;;){i;}
  foreach(int i, char* s; sarr){i;}
}
</code>

The same holds, when the declaration of `i' is omitted but `int ì' is supplied as a parameter to func5().

Because of the stated equivalence of the for-statement to a block- statement, at least for the for-statement an error has to be generated.

IMO for the foreach-statement an error has to be generated too.

Then not only the doc has to be updated, but the compiler as well.


Moreover: the reason for not allowing the hiding of local variables in
functions is stated as:
| While this might look unreasonable, in practice whenever this is
| done it either is a bug or at least looks like a bug.

This neglects the fact, that code is sometimes built by copying the block of a function as a block-statement into another function.

When D restricts this behaviour by design, then there is no reason to
allow the hiding of local variables within nested functions, i.e. the
comments in the following code should hold:
<code>
void func5(int i)
{
  void func6(int i) //error: parameter hides func5.i
  {
    i;
  }

  void func7()
  {
    int i;          //error: declaration hides func5.i

    i;
  }
}
</code>

By imposing this the fact holds, that at any point of the code there are at most two instances of one identifier in scope, one at the module level, reachable by the module scope operator `.', and another somewhere in the surrounding scopes, reachable directly.


So long.
-- 
Fight Spam! Join EuroCAUCE: http://www.euro.cauce.org/ 2EA56D6D4DC41ABA311615946D3248A1