[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: continuing continue - was Re: [patch] continue statement
- From: Glenn Maynard <glenn@...>
- Date: Sat, 24 Sep 2005 15:33:17 -0400
On Sat, Sep 24, 2005 at 01:59:52PM +0100, David Given wrote:
> I know someone who writes code like that. The reason, however, is to make it
> easy to do cleanup:
FWIW, the particular code I'd seen wasn't for cleanup: as I recall, it
did it in lots of functions with no cleanup.
> I would *love* a continue statement in Lua --- I find myself wanting it
> frequently. 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. 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;
c = foo();
if(!e) goto error;
if(a != NULL) destroy(a);
if(b != NULL) destroy(b);
if(c != NULL) destroy(c);
I prefer C++, where I can let dtors do the work. In Lua, I'd expect the
GC to do all this, but if you need immediate, predictable cleanup that's
sometimes not good enough.