lua-users home
lua-l archive

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


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

jdarling@eonclash.com wrote:
[...]
> If I have: GetWidth and SetWidth is their a way to have a table entry of
> Width that calls GetWidth and SetWidth automatically?  I know I have to
> play with __index, but I can't seem to wrap my brain around it.

__index is called when reading from a non-existent table entry,
__newindex when writing to one. So you can use a metatable of:

{
  __index = function(t, k)
    if (k == "width") then
      return GetWidth()
    end
  end,

  __newindex = function(t, k, v)
    if (k == "width") then
      SetWidth(v)
    end
  end
}

You have to make sure never to set t.width, though, or these will never
get called.

- --
+- David Given --McQ-+
|  dg@cowlark.com    | "Those that repeat truisms, are also forced to
| (dg@tao-group.com) | repeat them." --- Anonymous from Slashdot
+- www.cowlark.com --+
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFELT2zf9E0noFvlzgRAleAAJ91gXUAjfvuNQez9J5Elc8hL4y/FACgpm6g
99eOGwXsjTIQ2VO5yfu94tw=
=rdeP
-----END PGP SIGNATURE-----