[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Re: Functions as first-class objects
- From: Dirk Laurie <dirk.laurie@...>
- Date: Sun, 2 Sep 2018 08:28:06 +0200
Op So., 2 Sep. 2018 om 01:41 het Alejandro Reimondo
<aleReimondo@smalltalking.net> geskryf:
> In object paradigm (Object Technology) the only way to
> interact with an object is sending messages.
The Lua way to send messages to a running function is the yield-resume
mechanism for coroutines.
You create a controller for your function by
controller = coroutine.wrap(func)
After that, the main program and the function communicate like this:
(in main): answer1, answer2 = coroutine.resume(controller,...)
The controller calls func(...), which does
(in func): message1, message2 = coroutine.yield(answer1,answer2)
returning the answers and wating for the messages.
(in main): answer3, answer4 = coroutine.resume(controller,message1,message2)
The controller, having digested answer1 and answer2, passes the
messages to 'func' and waits for new answers.
(in func): message3, message4 = coroutine.yield(answer3,answer4)
returning the answers and wating for new messages.
Only one thread actually runs at a time, the other one waits.
Coroutines can't be used to achieve true concurrency, only to
temporarily switch execution from the calling thread to a wrapped
function and back.
The reason for all this is that Lua code can't be interrupted, it can
only be aborted. The Lua interpreter can be interrupted, and it can
then abort any running Lua code, but the Lua interpreter is a C
program.