lua-users home
lua-l archive

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

On Fri, Sep 24, 2010 at 11:44 AM, Rebel Neurofog
<> wrote:
>> Nothing wrong with it, using closures is a fine, if expensive, way of doing things.
> Expensive for memory and cheaper for speed.

You can mitigate the cost a little by creating the functions
themselves outside the constructor function, and creating functions
inside that simply call those functions. You save the cost of
recreating each function's bytecode by saving the functions in one
place; comparably, the "thunk" functions are a relatively minor
overhead as they simply do the work of passing the specific object to
the functions:

local function add(self, a, b)
  return a, b

function Calculator()
  local self = {}

  self.add = function(...)
    return add(self, ...)