lua-users home
lua-l archive

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


I'm using the code from: http://lua-users.org/wiki/InheritanceTutorial

If I understand properly then I'll want to modify the code quite a bit
to allow for my stuff to work.  For example right now they use:

  local new_class = {}
  local class_mt = { __index = new_class }
  newinst = {}
  setmetatable( newinst, class_mt )

If I understand properly I'll want to modify thusly:
  local new_class = { __index = MyFindGetter, __newindex = MyFindSetter
}
  local class_mt = { __index = new_class }
  newinst = {}
  setmetatable( newinst, class_mt )

  function MyFindGetter(self, PropertyName)
    if PropertyName=='Width' then
      return GetWidth(self.ID)
    end
    return new_class.__index(self)
  end

  function MyFindSetter(self, ProeprtyName, Value)
    if PropertyName=='Width' then
      SetWidth(self.ID, Value)
    else
      new_class[PropertyName] = Value
    end
  end

Or am I completely off on this?

 - Jeremy

"Help I suffer from the oxymoron Corporate Security."


> -------- Original Message --------
> Subject: Re: Little help with Getters, Setters and Native integration
> From: David Given <dg@cowlark.com>
> Date: Fri, March 31, 2006 8:33 am
> To: Lua list <lua@bazar2.conectiva.com.br>
> 
> -----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-----