lua-users home
lua-l archive

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


Le mer. 28 nov. 2018 à 11:06, Egor Skriptunoff <egor.skriptunoff@gmail.com> a écrit :
But the next question from Sony L. would be the following:
How to save intermediate value in this code
a:b.c()
a:b.d()

This idea a very strange object type in Lua creating an object that references two:
 
to rewrite it in optimized way
local x = a:b
x.c()
x.d()

The way I percieve it is that "x" should be a "proxying" function object (which internally will call function "b"), defined in a closure that holds the value "a". Something like:

local x = function
  local __o___ = a;
  return function(...)
    return b(__o__, ...)
 end
end
x(c)
x(d)

This adds the overhead of one level of function call (even if this is a trailing call that will not consume stack space)