lua-users home
lua-l archive

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


I need "properties" that send notifications to listeners on value
change. Since I want to avoid to have to write "notify(xxx, yyy)"
after each property change I thought of something like this:

===================== https://gist.github.com/746925
local lib = {}
Property = lib

setmetatable(lib, {
 __call = function(table)
  local instance = {prop = {}}
  setmetatable(instance, lib)
  return instance
end})

function lib.__newindex(table, idx, value)
  rawset(table.prop, idx, value)
  -- notify value change
  print(idx, value)
end

function lib.__index(table, idx)
  return rawget(table.prop, idx)
end

x = Property()

x.foo = 5
x.foo = 6
print(x.foo)

=====================

What is the performance impact of such an implementation ?

Gaspard