why scope(success)?

James Dunne james.jdunne at gmail.com
Thu May 11 17:49:59 PDT 2006


Sean Kelly wrote:
> Ben Hinkle wrote:
> 
>> "Stewart Gordon" <smjg_1998 at yahoo.com> wrote in message 
>> news:e3vkhn$1kq5$1 at digitaldaemon.com...
>>
>>> James Dunne wrote:
>>> <snip>
>>>
>>>> Then the implementation according to D's language specs is 
>>>> incorrect. Nothing is mentioned about new scopes created by 
>>>> if-statements or while-statements.  New scopes are only created from 
>>>> block { } statements when inside a function body.  Scope is 
>>>> mentioned (in passing) for the for-statement; the initializer is 
>>>> noted as a special case.  Did I miss a blanket statement somewhere 
>>>> else in the docs about this?
>>>
>>> I think you're meant to use a bit of common sense here.  What sense 
>>> does it make for a declaration to be conditional at runtime?
>>>
>>> To be honest, I think a naked declaration as the body of a runtime 
>>> control statement should be illegal.
>>
>>
>> You're probably right. I had tried dmc and cl and they both complained 
>> about the use of 'a' without a declaration but in fact I was expecting 
>> an error that a declaration can't be the body of an 'if' statement. 
>> Since both compiler didn't say boo about the declaration I figured 
>> that declarations are considered statements. The C99 spec doesn't 
>> consider a declaration a statement so I suspect the compilers are 
>> giving poor errors. 
> 
> 
> What about this:
> 
>     if( auto i = doSomething() )
>         printError( i );
> 
> I don't think anyone would expect 'i' to survive after the printError 
> expression.
> 
> 
> Sean

Straight from the docs:
"If an auto Identifier is provided, it is declared and initialized to 
the value and type of the Expression. Its scope extends from when it is 
initialized to the end of the ThenStatement."

It's buried in there that an implicit scope is created for these cases, 
but nothing whatsoever is said about scope for a straight "if 
(expression) not-block-statement".

As you noted earlier, this is a corner case where one would expect a 
declaration-statement to be illegal as an if-statement's body. 
Declaration-statements really aren't statements, they're just parsed 
that way to alleviate complexity.

-- 
Regards,
James Dunne



More information about the Digitalmars-d mailing list