lua-users home
lua-l archive

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


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

Mark Hamburg wrote:
[...]
> I used to really want this and even made proposals in this regard. I've gone
> back and studied the subtleties of Smalltalk blocks and realized that the
> problem is a lot deeper than one would expect.
[...]
> Does the return exit the function and hence behave differently from a return
> in:

One thing you can do in Smalltalk is to have the block return *after* the
enclosing method has terminated! Consider:

handleClicksOn: o
	o whenButtonClickedDo: [ ^ true ].
	o enterMainLoop

(Calling whenButtonClickedDo: registers a callback on o that returns true;
[...] defines a closure, ^ is return. enterMainLoop blocks and handles events.
Smalltalk is, in a lot of ways, a very *concise* language.)

What happens if the main loop exits, handleClicksOn: terminates, and then
somehow the user manages to click the button? Well, the standard doesn't
actually say --- it's 'undefined'. The same thing applies if you manage to
pass the closure off to a thread other than the one that actually created it.

It's actually rather hard to know what to do in this situation. Throw an
exception? Except that exceptions are processed along the dynamic call chain,
and it isn't the fault of the code that's trying to *use* the closure that it
went wrong. You can't do anything along the static chain, either, because the
closure's home method no longer exists --- if it did, this wouldn't be a problem.

(It's worth mentioning that you *cannot* emulate this kind of thing using
exceptions, due to dynamic-vs-static issues. You will need true non-local-goto
support to make it work. Something like:

function handleClicksOn(o)
	local lj, v = setjmp()
	if lj then
		-- setjmp returned marker
		o:whenButtonClickedDo(
			function()
				lj:longjmp(v)
			end)
		o:enterMainLoop()
		-- if we got here, the callback never fired
		return nil
	else
		-- if lj is nil, then lj:longjmp() was called
		return v
	end
end

.)

- --
┌── 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

iD8DBQFGywZEf9E0noFvlzgRAndbAJ45LWNckPi8R2EiT8LZ02BeLgJ34gCfaLKq
K2XnIc5S+NcUS4Oy/N1yMdc=
=Otab
-----END PGP SIGNATURE-----