lua-users home
lua-l archive

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


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Tim Hunter wrote:
[...]
> Is there any way for the function f to modify o.test? In my code there
> could be multiple o tables, all sharing the t metatable. I would like
> function f to store state information in each individual o table.

The way your code is set up, no, there isn't --- that's because you actually
have two metatables, one of which is hooked up to the other. o's metatable is
a table with a metatable whose __index is doing the work. By the time you get
to the second metatable, the context is lost.

Was this intentional? Because if it wasn't, you can just use a single
metatable, and the context is passed in as the first parameter to __index:

m = {__index =
  function(x, k)
    local function f()
      print("You called " .. k)
      x.test = x.test + 1
    end
    return f
  end
}

t = setmetatable({test = 1}, m)

print(t.test)
t.foo()
print(t.test)


- --
┌── dg@cowlark.com ─── http://www.cowlark.com ───────────────────
│
│ "There does not now, nor will there ever, exist a programming language in
│ which it is the least bit hard to write bad programs." --- Flon's Axiom
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFHB+xhf9E0noFvlzgRAvAeAKCiq/YCrMBf3SoFhLr59efwdx/ApgCePLum
uWqj0QSqpiAdxKCORz9Efu8=
=JeBm
-----END PGP SIGNATURE-----