Scope/block behaviour

Stewart Gordon smjg_1998 at yahoo.com
Thu Aug 19 06:46:34 PDT 2010


Eduardo Cavazos wrote:
> Hello,
> 
> I was surprised that these seem to not be allowed in D:
> 
> void main ()
> {
>   auto a = 20 ;
> 
>   {
>     auto a = 30 ;
>   }
> }

Correct.

http://www.digitalmars.com/d/1.0/statement.html#ScopeStatement
"Even though a new scope is introduced, local symbol declarations cannot 
shadow (hide) other local symbol declarations in the same function."

> void main ()
> {
>   { int f0 () { return 10 ; } }
> 
>   { int f0 () { return 20 ; } }
> }

Looks like a bug.

> Perhaps I missed something in the FAQ.
> 
> Is there anywhere (manual or TDPL) I can read up on this language design 
> decision? What other contemporary (or classic) languages feature this 
> behaviour? Scheme and C both allow the above.
> 
> It seems like this would be something that might be nice for certain 
> shops to enforce via a compiler switch, but not on by default.

What use case have you for this feature?

Many things are legal in C(++), but nearly always mistakes and so 
compilers may generate warnings about them.  This is another example. 
Generally, the route D has taken has been to make them illegal.  IINM, 
there are always workarounds for those cases where it really is what you 
meant.

Stewart.


More information about the Digitalmars-d mailing list