lua-users home
lua-l archive

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


Since Lua is a language that provides basic mechanisms instead of builtin high-level constructs, I am wondering:  why were to-be-closed variables given special language-level support?  I think it should be possible to implement the equivalent using closures:

|  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

I left out some details, but I'm assuming that something like the above could be made to work.  If not, what am I missing?  But either way, I am wondering what went into the decision to give to-be-closed variables language-level support instead of having users construct them from Lua's mechanisms?

I like the feature and I use it, but I am just wondering out of curiosity.