I have the following Class
local PROGRESS = {}
PROGRESS.__index = function(self,key)
if key~="__group" and self.__group[key] then
return self.__group[key]
else
return rawget(self,key)
end
end
What this does is when You access table[key]
it performs a lookup in table.__group
(which is an object of another class) and returns table.__group[key]
,if it is not nil.
Now I am trying to do the same for member functions.
i.e If I call table:key()
a lookup must be performed in table.__group
and if the function is present, then table.__group:key()
should be called.
How do I accomplish this?
I tried to do this.
local PROGRESS = {}
PROGRESS.__index = function(self,key)
if key~="__group" and self.__group[key] then
local val = self.__group[key]
if type(val) == "function" then
self.__group:val()
return function() end
end
return self.__group[key]
else
return rawget(self,key)
end
end
But there are 2 things wrong here.
- I am unable to retrieve the original function's arguments
- Event if I just ACCESS
table[key].function
without calling it, the function will be called
And I've got the feeling that I am trying to complicate things and the solution is way simpler.
Any help is appreciated.
UPDATE
@Mud The problem with the original code is that the object passed as 'self' to the member function is an object of the new class. Not of the old class.
Consider this code
GROUP_CLASS = {}
GROUP_CLASS.__index = GROUP_CLASS
function GROUP_CLASS:showSum (a,b) print(self);print(a + b) end
group_object = setmetatable({},GROUP_CLASS)
group_object:showSum(1,2)
local PROGRESS_CLASS = {}
PROGRESS_CLASS.__index = function(self,key,value)
if key~="__group" and self.__group[key] then
return self.__group[key]
else
return rawget(self,key)
end
end
progress_object = setmetatable( {__group = group_object} , PROGRESS_CLASS)
progress_object:showSum(3,3)
--progress_object is passed as first argument to showSum. But i need group_object to be passed
In the above code, When progress_object:showSum(3,3)
is called,
is it possible to pass group_object (or in other words progress_object.__group) as self instead of progress_object.
Hope that makes sense.
Response to updated post:
If you're going to ignore the state of the object a method is called on, and substitute the state of some other object, why is it even a method on that object? That's like overriding the addition operator to do multiplication, a recipe for confusion.
In other words, you want this:
To resolve, via bizarre internal machinery, into this:
Why not skip a step and just make the latter call?
If you must, you could achieve this by returning a proxy for the method which replaces
self
with__group
Response to original post:
Do nothing. Your original code handles this.
Lua doesn't know what a "member function" is. A member is a member (i.e. an element in a table), and whether the value of that member is a function is irrelevant.
Remember:
obj:method(a,b,c)
is exactly equivalent toobj.method(obj,a,b,c)
obj.method
is exactly equivalent toobj["method"]
.obj["method"]
intoobj.__group["method"]
So you're done.
For instance, say we have:
Using your first code, we can write:
That's it.
Now, as long as we're here, let's look at what your second function is doing:
First you grab the function value from
__group
. At this point you're done. Simply return that value, and the caller is going to call that value (i.e.(...)
). Instead, you call__group["val"]
which is likely a totally different function from__group[key]
(unless key=="val"), then you pass the caller a function which does nothing.