Goto skips declaration

Michelle Long HappyDance321 at gmail.com
Tue Oct 30 21:21:22 UTC 2018


On Tuesday, 30 October 2018 at 06:37:22 UTC, Bauss wrote:
> On Monday, 29 October 2018 at 03:24:35 UTC, rikki cattermole 
> wrote:
>> On 29/10/2018 4:22 PM, Michelle Long wrote:
>>> This should not be an error when the goto jumps outside the 
>>> current block!
>>> 
>>> {
>>>     goto Y;
>>>     int x;
>>> }
>>> Y:
>>> 
>>> There is no chance of any local variable being used. This 
>>> makes it impossible to use goto statements in any reasonable 
>>> way without littering the code with brackets.
>>> 
>>
>> Compiles + runs:
>>
>> import std.stdio;
>>
>> void main() {
>>     {
>>         goto Y;
>>         int x;
>>         writeln("booo", x);
>>     }
>>     Y:
>>     writeln("Hello D");
>> }
>>
>> Going to need a more complete example.
>
> The problem is when you remove the brackets.
>
> @OP
>
> I don't see what you expect the compiler to do? You're skipping 
> the declaration and usage of it so what's the point?
>
> D is not Javascript and has no concept of hoisting.

You take examples WAY to literal. Sheesh, do you really think 
life is that simple? Do you need every detail spelled out for 
things to make sense?

foreach(x; Y)
{
     if (something or another which I won't spell out no matter 
how much you cry)
         goto Q; // compiler complains here about x
     int x;
}
Q:

There is no reason why the compiler should have a problem with 
this.. the fact that using brackets around int x; satisfies the 
compiler proves this.







More information about the Digitalmars-d mailing list