lua-users home
lua-l archive

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


Am 21.10.2014 um 19:11 schröbte Andre Arpin:
This post is long so I remove all quotes
Note: the unexpected value of self when o.baz is invoked.

This is exactly the same problem as before: `o:baz()` involves two distinct operations: one `__index` call to retrieve the `baz` function, and finally one call to that `baz` function. The `__index` function and the `baz` function may (and in this case will) be called with different `self` arguments. You are trying to do all the work in the `__index` function alone:


setmetatable(getmetatable(metainstance.__index).__index, {
         __index = function(self, key)

This `self` is always the same as `getmetatable(metainstance.__index).__index`, *not* `o`.

             print(self, key, "self, key")
             dump(self, 'self')
             for k,v in pairs(self) do
             	print(k, v, 'k,v')
             end
             if key == "baz" then
                 print(type(self), (self).v)
                 return rawget(self,'v')

Here you are trying to access `o` (the object before the colon operator in `o:baz()`), but it is simply not available here. You have to return a function reference at this point, which in turn will be called with the `self` value you expect (i.e. `o`).

             end
         end,
     })


Philipp