Asko Kauppi wrote:
[...]
I've long wanted to have seeming parallelism in Lua, something
akin to
DBUS-kind message passing but made to be syntactically so "sweet" it
would seem native, transparent, and noncluttered in the code.
Lately, I
crafted a mechanism that would make such calls seem like regular
function calls, using userdata as handles to the returned values.
[searches on Wikipedia]
Aha. I thought this concept seemed familiar.
http://en.wikipedia.org/wiki/Future_%28programming%29
Guy I knew at university wrote an entire language based around
futures (and
Linda-based process synchronisation, which is extremely neat but
not yet
actually *useful*). It worked in much the same way you describe,
but had
language support so that you didn't need any extra syntax to access
the value
of the future.
For Lua, given that we'd need syntactic help anyway, I'd be more
inclined to
use actual objects. So, lane(f) creates a new thread object; calling
o:result() (or possibly just accessing o.result) would block until
the thread
returned a value. It would probably be sensible to allow the thread
to return
multiple values, too.
function thread()
for i=1, 10 do lane.return(i) end
end
lo = lane(thread)
while lo:running() do
print(lo:result())
end
Hmm. I think we may be reinventing Communicating Sequential
Processes...
--
┌── dg@cowlark.com ───
http://www.cowlark.com
───────────────────
│ "I have always wished for my computer to be as easy to use as my
│ telephone; my wish has come true because I can no longer figure
out how to
│ use my telephone." --- Bjarne Stroustrup