lua-users home
lua-l archive

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


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Patrick Donnelly wrote:
[...]
> What features would you like to see added?

I'd like a goto.

The whole goto-considered-harmful myth has done the world a vast disservice.
goto is *entirely* the right construct for dealing with certain software
problems: for example, state machines. (And, in fact, Dijkstra was completely
up front about this in his original paper.) They're also very handy for doing
things like cleanup on error:

	if not function1() then
		goto exit
	end
	if not function2() then
		goto cleanup1_and_exit
	end
	if not function3() then
		goto cleanup2_and_exit
	end
	return success

	cleanup3()
cleanup2_and_exit:
	cleanup2()
cleanup1_and_exit:
	cleanup1()
	return failure

The alternative approach is to use lots of nested if statements, which can
quickly get out of control if your logic is at all complicated --- and if you
need an exit strategy from within, say, a loop, you're basically stuffed. You
have to either duplicate code or use flag variables.

(They can also be trivially used for 'continue' and multi-level 'break'.)

...and while I'm whinging, it would also be nice to have some
try/catch/finally/throw syntactic sugar around pcall and error. I know it's
Not The Lua Way to provide high-level functionality like that, but exceptions
are so useful, and doing them yourself with pcall is so wordy, that I reckon
it's worth doing in this case.

- --
┌── dg@cowlark.com ─── http://www.cowlark.com ───────────────────
│
│ "There does not now, nor will there ever, exist a programming language in
│ which it is the least bit hard to write bad programs." --- Flon's Axiom
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGxZ14f9E0noFvlzgRAvhBAJ9x8IcZm9vnUTJ6hlJo11W0C7723wCeLNaU
RUSmsCHdCwuKC357y04HTeY=
=KcSr
-----END PGP SIGNATURE-----