lua-users home
lua-l archive

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



On 1-Dec-06, at 12:31 AM, wang xu wrote:

Is there a generic way to automatically update one variable when the value of another variable is changed?

No. (And why do you want to do that?)

The closest you can do is to implement your own "boxed" values, using tables with a single key (say, 1):

val1 = {"a"}
val2 = val1
val1[1] = "b"
=val2[1]

You could dress it up in O-O notation:

do
  local meta = {}
  meta.__index = meta
  function meta:set(v) self[1] = v end
  function meta:get() return self[1] end

  function Box(v)
    return setmetatable({v}, meta)
  end
end

val1 = Box("a")
val2 = val1
=val2:get()
val1:set("b")
=val2:get()