lua-users home
lua-l archive

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


On Sat, Sep 10, 2022 at 12:54 AM David Sicilia <dpsicilia@gmail.com> wrote:
I made post this list some time ago claiming that the <close> mechanism could have been implemented with Lua's existing mechanisms like so:
[...]
|  function with(expr, func)
|    ... = pcall(func, expr)
|    getmetatable(expr).__close(expr)
|    -- propagate errors and/or return result
|    ...
|  end

I must admit, having this topic raised was the last thing I expected ;)

Once I wrote [1]:

I'm well versed in trying to use Lua function lambdas for this kind of problem.  The result is not satisfying. 

The problem with lambdas masquerading as code blocks is that the natural control structures of the language become unusable, or very confusing.   Think about what `return` and `break` mean in the context of a lambda vs. a real code block.  Preserving the intended use of basic control structures is essential for keeping code manageable.

Sure, you could write any Lua program as a single block of code and only `goto` (giving up functions, `while` loops, etc.), but that sounds like 1960's assembly language, not Lua.  The goal was to support scope hooks while preserving all that Lua is.

[1] http://lua-users.org/lists/lua-l/2018-07/msg00365.html