August 19, 2014
On Monday, 18 August 2014 at 13:51:14 UTC, nrgyzer wrote:
> 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;
> }

For what it's worth, whenever you have "goto-end" style code, place all your code in a proper block, in such a way that all your variable declarations are in that block, and all your gotos break out of this block. This way, a goto will *never* cross a declaration, so coding is easy. The only variables you place at the top or the ones that could need cleanup.

void main(string[] args)
{
	//Declarations that need cleanup:
	void* p;

	//Code
	{
		BigInt i = "12345"; //Local variable
		if (args.length > 1)
		{
			goto Exit; //Breaks out of block
		}
		i = BigInt("67890");
		BigInt j = "54321"; //Local variable
	}

	//End
	Exit:
		CleanUp(p);
		return;
}
August 21, 2014
On Tuesday, 19 August 2014 at 20:33:00 UTC, monarch_dodra wrote:
> On Monday, 18 August 2014 at 13:51:14 UTC, nrgyzer wrote:
>> 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;
>> }
>
> For what it's worth, whenever you have "goto-end" style code, place all your code in a proper block, in such a way that all your variable declarations are in that block, and all your gotos break out of this block. This way, a goto will *never* cross a declaration, so coding is easy. The only variables you place at the top or the ones that could need cleanup.
>
> void main(string[] args)
> {
> 	//Declarations that need cleanup:
> 	void* p;
>
> 	//Code
> 	{
> 		BigInt i = "12345"; //Local variable
> 		if (args.length > 1)
> 		{
> 			goto Exit; //Breaks out of block
> 		}
> 		i = BigInt("67890");
> 		BigInt j = "54321"; //Local variable
> 	}
>
> 	//End
> 	Exit:
> 		CleanUp(p);
> 		return;
> }

Yes, that works. I'm using the goto-command to exit my function
if an error (not necessarily an exception) occured. Sure, I can
simply do a return, but I personally prefer a fixed endpoint of
my functions. This also simplifies debugging my application
because I don't need 10 or 20 breakpoints (one before each
return-point).
August 21, 2014
On 08/21/2014 04:12 AM, nrgyzer wrote:

> I'm using the goto-command to exit my function
> if an error (not necessarily an exception) occured.

Sorry to repeat myself but if an exception occurs in code before the goto, the exit code will not be executed. Of course, it may be that the function is defined 'nothrow' so my concern does not apply.

> Sure, I can
> simply do a return, but I personally prefer a fixed endpoint of
> my functions.

Again, that is not possible in a language that has exceptions.

Ali

August 21, 2014
On Thursday, 21 August 2014 at 17:39:16 UTC, Ali Çehreli wrote:
> On 08/21/2014 04:12 AM, nrgyzer wrote:
>
> > I'm using the goto-command to exit my function
> > if an error (not necessarily an exception) occured.
>
> Sorry to repeat myself but if an exception occurs in code before the goto, the exit code will not be executed. Of course, it may be that the function is defined 'nothrow' so my concern does not apply.
>
> > Sure, I can
> > simply do a return, but I personally prefer a fixed endpoint
> of
> > my functions.
>
> Again, that is not possible in a language that has exceptions.
>
> Ali

Sure, but what about the following pretty simple source:

bool testa(int a)
{
    bool fIsValid = (a > 0);

    if (a > 0)
       goto Exit;

    throw new Exception("int <= 0");
Exit:
    return fIsValid;
}

int main(string[] args)
{
    testa(10); // Use goto
    testa(-1); // Throws an exception
}

I'm absolutely aware that gotos are useless if an exception
occured before the goto appears. But in some cases they are very
useful, especially when I want prevent 10 ident-steps or
something else...

Okay, my question was answered -> compiler bug. Thanks in advance!
1 2
Next ›   Last »