lua-users home
lua-l archive

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

Thanks for that, looking forward to reading it.  Curious to know your thoughts on the following.  I made post this list some time ago claiming that the <close> mechanism could have been implemented with Lua's existing mechanisms like so:

|  do
|    local x<close> = CloseMe1()
|    local y<close> = CloseMe2()
|    print( x, y )
|    return 5
|  end

can be implemented as e.g.:

|  return with( CloseMe1(), function( y )
|    return with( CloseMe2(), function( x )
|      print( x, y )
|      return 5
|    end)
|  end)


|  function with(expr, func)
|    ... = pcall(func, expr)
|    getmetatable(expr).__close(expr)
|    -- propagate errors and/or return result
|    ...
|  end

by copying the concept of "with" taken from Python.  I understand that this wouldn't be equivalent to close completely, in that one could not take a suspended coroutine and call "close" on it to immediately close all to-be-closed variables on the stack.  But, assuming that is not needed, would the above work for your use cases?


On Fri, Sep 9, 2022 at 10:58 AM John Belmonte <> wrote:
I'm sharing a draft article "Structured concurrency and Lua (part 1)".

After the long road of lobbying for scope exit hooks in Lua (begun around the time of the Lua Gems book, in 2008), it's been nice to finally have the feature and apply it to problems.  What I couldn't imagine in 2008, however, is that one would ever want to yield from scope exit.  But Lua can do that now too, enabling interesting possibilities, such as the structured concurrency presented in the article.

Feedback is welcome here or on github.  (Encouragement also, if you'd like to see the series continue.)