lua-users home
lua-l archive

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

On 5/30/07, Jerome Vuarand <> wrote:
John Labenski wrote:
> print(udata.GetStuff)
> print(udata.GetStuff())
> print(udata:GetStuff())
> udata.SetStuff(5)
> udata.SetStuff = 5
> udata:SetStuff(5)
> [...]
> I am most interested in knowing the difference between udata.GetStuff
> <==> udata:GetStuff() udata.SetStuff = 5 <==> udata:SetStuff(5)

You are mixing syntatic sugars, metamethods and a confused idea of how
userdata setters/getters work. Here is equivalent code, without the
syntax sugar and with explicit metamethod access:

Well, yes, things are mixed and these are all equivalent to C++ class
member functions not variables. The sugar (if I'm using that right)
that people want to use is that you can access Set/GetXXX functions
like variables, but without the "Get" and "Set". I'm personally not a
big fan of it, but yes it can and does work. Also note that GetXXX
must not take any values and return one and SetXXX must take and
return only one.

(ps. yes I do use __index and __newindex, but used __index as an
example, I hope the above doesn't confuse things even more.)

udata.Stuff <= is the same as => udata:GetStuff()
udata.Stuff = 5 <= is the same as => udata:SetStuff(5)

The bindings for wxLua generate structures to know about all 4 of the
methods above, but I'm trying to be "clever" and within my __index and
__newindex C functions determine on the fly what to do. Note that, for
example the __index case, the udata.Stuff calls the exact same C
function as udata:GetStuff() after first removing the string "Stuff"
so that the stack is identical as the call to GetStuff(), something
similar happens for the __newindex case.

Yeah, it's a little funny (to me at least), but some people like it.

If something is still not clear, fell free to ask for more details :-)

So... can you determine if () was on the call to __(new)index and that
a function is expected?

Or that a '.' vs. a ':' was used? (this would be useful for static C+
member functions where we don't need the userdata at all and would
like to lua_remove() it if the user used the ':' notation.)

   John Labenski