Jump to page: 1 2
Thread overview
goto skips declaration of variable
Aug 18, 2014
nrgyzer
Aug 18, 2014
bearophile
Aug 18, 2014
bearophile
Aug 18, 2014
ollie
Aug 18, 2014
Ali Çehreli
Aug 18, 2014
ketmar
Aug 19, 2014
nrgyzer
Aug 19, 2014
ketmar
Aug 19, 2014
bearophile
Aug 19, 2014
Ali Çehreli
Aug 19, 2014
monarch_dodra
Aug 21, 2014
nrgyzer
Aug 21, 2014
Ali Çehreli
Aug 21, 2014
nrgyzer
August 18, 2014
Hi all,

I've the following code snipped:

import std.bigint;
void main(string[] args)
{
	BigInt i = "12345";
	if (args.length > 1)
	{
		goto Exit;
	}
	i = BigInt("67890");
	Exit:
		return;
}

When I try to compile this sample application I'm getting the following error:

sample.d(7): Error: goto skips declaration of variable sample.main.__ctmp1344 at sample.d(9)

I know that the error is caused by the line 'i = BigInt("67890")'. I also know that I can simply replace the goto-condition with something like this:

if (args.length == 0)
{
	i = BigInt("67890");
}

But that's not what I want because I've many BigInt-assignments in my real application. That would result in a very very deep, nested source structure and that really hurts. So, is there any solution how I can realize the BigInt-assignment after the goto-Command (or better: without skipping the declaration)?
August 18, 2014
nrgyzer:

> import std.bigint;
> void main(string[] args)
> {
> 	BigInt i = "12345";
> 	if (args.length > 1)
> 	{
> 		goto Exit;
> 	}
> 	i = BigInt("67890");
> 	Exit:
> 		return;
> }
>
> When I try to compile this sample application I'm getting the following error:
>
> sample.d(7): Error: goto skips declaration of variable sample.main.__ctmp1344 at sample.d(9)

It looks like a compiler bug (perhaps caused by a rewrite rule). The 'i' variable is declared before the goto.

Bye,
bearophile
August 18, 2014
> It looks like a compiler bug

https://issues.dlang.org/show_bug.cgi?id=13321

Bye,
bearophile

August 18, 2014
On Mon, 18 Aug 2014 13:51:12 +0000, nrgyzer wrote:

> When I try to compile this sample application I'm getting the following error:
> 
> sample.d(7): Error: goto skips declaration of variable sample.main.__ctmp1344 at sample.d(9)
> 

http://dlang.org/changelog.html#disable_goto_skips_init

First item in D2.065 Changelog for language changes :
  1. Goto jumps now cannot skip variable declarations
August 18, 2014
On 08/18/2014 09:07 AM, ollie wrote:
> On Mon, 18 Aug 2014 13:51:12 +0000, nrgyzer wrote:
>
>> When I try to compile this sample application I'm getting the
>> following error:
>>
>> sample.d(7): Error: goto skips declaration of variable
>> sample.main.__ctmp1344 at sample.d(9)
>>
>
> http://dlang.org/changelog.html#disable_goto_skips_init
>
> First item in D2.065 Changelog for language changes :
>    1. Goto jumps now cannot skip variable declarations
>

So, that is the change that caused this regression. nrgyzer's code does not violate that rule.

Ali

August 18, 2014
On Mon, 18 Aug 2014 13:51:12 +0000
nrgyzer via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com>
wrote:

> When I try to compile this sample application I'm getting the following error:
> 
> sample.d(7): Error: goto skips declaration of variable sample.main.__ctmp1344 at sample.d(9)
it's compiler bug i believe.

but why do you need gotos at the first place? i'm not saying that "you
must avoid gotos at all costs!", but D has some nice features like
scope(exit), scope(success), scope(failure) and nested functions, which
can render gotos unnecessary in many cases (and make code cleaner).


August 19, 2014
On Monday, 18 August 2014 at 17:47:21 UTC, ketmar via Digitalmars-d-learn wrote:
> On Mon, 18 Aug 2014 13:51:12 +0000
> nrgyzer via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com>
> wrote:
>
>> When I try to compile this sample application I'm getting the following error:
>> 
>> sample.d(7): Error: goto skips declaration of variable sample.main.__ctmp1344 at sample.d(9)
> it's compiler bug i believe.
>
> but why do you need gotos at the first place? i'm not saying that "you
> must avoid gotos at all costs!", but D has some nice features like
> scope(exit), scope(success), scope(failure) and nested functions, which
> can render gotos unnecessary in many cases (and make code cleaner).

I know, gotos are having a negative connotation. Sure, I can also use nested functions, but in my opinion it results in dirty and complex code. It's totally overkilled compared to a simple if and goto-instruction. The same regards the scoping... it's simply to much overhead.
August 19, 2014
On Tue, 19 Aug 2014 08:04:54 +0000
nrgyzer via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com>
wrote:

> goto-instruction. The same regards the scoping... it's simply to much overhead.
it's a matter of taste, i think. i myself find 'scope(exit)' excellent, 'cause i'm always keep forgeting to free some resources / restore some state in "EXIT:" part. ;-)

but i'm not saying that everyone should avoid gotos, and this is definitely the bug in compiler that should be fixed.


August 19, 2014
nrgyzer:

> Sure, I can also use nested functions, but in my opinion it results in dirty and complex code. It's totally overkilled compared to a simple if and goto-instruction.

Often nested functions are less complex, more clean and simpler code compared to using gotos. I suggest to start using nested functions and see how they work out.

Bye,
bearophile
August 19, 2014
On 08/19/2014 01:04 AM, nrgyzer wrote:

> On Monday, 18 August 2014 at 17:47:21 UTC, ketmar via

>> but why do you need gotos at the first place? i'm not saying that "you
>> must avoid gotos at all costs!", but D has some nice features like
>> scope(exit), scope(success), scope(failure) and nested functions, which
>> can render gotos unnecessary in many cases (and make code cleaner).
>
> I know, gotos are having a negative connotation. Sure, I can also use
> nested functions, but in my opinion it results in dirty and complex
> code. It's totally overkilled compared to a simple if and
> goto-instruction. The same regards the scoping... it's simply to much
> overhead.

However, goto is not a substitute for scope(exit) and friends or similar D features because goto may not be executed if an exception is thrown.

Ali

« First   ‹ Prev
1 2