[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Features you would like to see
- From: David Given <dg@...>
- Date: Tue, 21 Aug 2007 16:35:32 +0100
-----BEGIN PGP SIGNED MESSAGE-----
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
One thing you can do in Smalltalk is to have the block return *after* the
enclosing method has terminated! Consider:
o whenButtonClickedDo: [ ^ true ].
(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:
local lj, v = setjmp()
if lj then
-- setjmp returned marker
-- if we got here, the callback never fired
-- if lj is nil, then lj:longjmp() was called
┌── ｄｇ＠ｃｏｗｌａｒｋ．ｃｏｍ ─── 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
-----END PGP SIGNATURE-----