lua-users home
lua-l archive

Re: continuations (was Re: error() or nil, 'error msg')

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


On Tuesday, September 30, 2014 01:02:37 PM Coda Highland wrote:
> No, the goto B is skipping over the normal control flow to continue
> execution at the label. Think of it more like an exception than a
> coroutine:
> 
> function A()
> local N=0
> local C=function()
> N=N+1
> error("goto B")
> end
> pcall(function() N=1+C() end)
> -- ::B:: goes here
> return N
> end
> print(A())
> 
I could also rewrite it this way:
 function A()
 local N=0
 local B=function() return N end
 local C=function()
 N=N+1
 return B()
 end
 N=1+C()
 return B()
 end
A tail call replaces the current call with a different context. Goto also 
replaces the current context. Right now the limitation of goto is that the 
context must be within the same function and not introduce any new locals. A 
tail-call-goto relaxes the restriction to allow the function to change.
A continuation-goto on the other hand, is not only changing the context but 
unwinding the call stack. What happens, then, if A is not in the call chain?
 function setjmp(F)
 longjmp = F
 return true
 end
 function A()
 N=1
 if setjmp(function() goto two end) then
 return N
 end
 ::two::
 return N+1
 end
 function B()
 longjmp()
 return 0
 end
 print(A())
 print(B())
Does B() print 2, 0, or is it an error because A is not in the call stack?
-- 
tom <telliamed@whoopdedo.org>

AltStyle によって変換されたページ (->オリジナル) /