lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Glenn Maynard wrote:
On Sat, Sep 24, 2005 at 01:59:52PM +0100, David Given wrote:
I would *love* a continue statement in Lua --- I find myself wanting it frequently.

Funny, I often use break statements, rarely continue.
Well, both have their uses.

For that matter, I'd rather like goto, as well, but I probably
shouldn't admit that in public.

There's nothing inherently wrong with goto, used judiciously.  The
"goto considered harmful" folks are guilty of claiming that a language
feature is always bad, which is almost always false.

The most obvious way of doing the above with goto is to have five labels,
interleaved through the cleanup code, which is very messy.

Why is it messy?

 A nice and
clean way of doing it, though (in C) is:

	void *a = NULL, *b = NULL, *c = NULL, *d = NULL;
	a = foo();
	if(!a) goto error;

	b = foo();
	if(!c) goto error;

Typo... Not important to the discussion.

	c = foo();
	...

	if(!e) goto error;
	do work;
	return;

	error:
	if(a != NULL) destroy(a);
	if(b != NULL) destroy(b);
	if(c != NULL) destroy(c);
	...

Which suppose foo() is returning NULL in case of error...

I kind of like:
void *a = NULL, *b = NULL, *c = NULL, *d = NULL;
a = foo();
if (a == NULL) goto errorA;

b = foo();
if (b == NULL) goto errorB;

c = foo();
...

if (e == NULL) goto errorE;
do work;
return;	// We may want to do through...
...

destroy(c);
errorC:

destroy(b);
errorB:

destroy(a);
errorA:

When possible, I avoid mixing true boolean with NULL/0 values, which you do in your cleanup code.

My code avoids tests, which is interesting only in time critical applications, I suppose.

IMHO, both codes are OK, your may be better suited to complex destroy cases.

--
Philippe Lhoste
--  (near) Paris -- France
--  http://Phi.Lho.free.fr
--  --  --  --  --  --  --  --  --  --  --  --  --  --