Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8142259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:45:22+00:00 2026-06-06T12:45:22+00:00

I have the following Class local PROGRESS = {} PROGRESS.__index = function(self,key) if key~=__group

  • 0

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.

  1. I am unable to retrieve the original function’s arguments
  2. 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T12:45:24+00:00Added an answer on June 6, 2026 at 12:45 pm

    Response to updated post:

    progress_object is passed as first argument to showSum. But i need group_object to be passed

    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:

    progress_object:method("foo")
    

    To resolve, via bizarre internal machinery, into this:

    group_object:method("foo")
    

    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

    local PROGRESS_CLASS = {}
    PROGRESS_CLASS.__index = function(self,key)
      local groupval = self.__group[key]
      if key == '__group' or not groupval then
        return rawget(self,key)
      elseif type(groupval) ~= 'function' then
        return groupval
      else
          return function(...)
            if self == ... then -- method call
              -- replace self argument with __group
              return groupval(self.__group,select(2,...))
            else
              return groupval(...)
            end
          end
      end
    end
    

    Response to original post:

    How 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?

    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:

    1. obj:method(a,b,c) is exactly equivalent to obj.method(obj,a,b,c)
    2. obj.method is exactly equivalent to obj["method"].
    3. Your code already resolves obj["method"] into obj.__group["method"]

    So you’re done.

    For instance, say we have:

    group = {}
    function group:showSum    (a,b) print(a + b) end
    function group:showProduct(a,b) print(a * b) end
    

    Using your first code, we can write:

    foo = setmetatable({__group = group}, PROGRESS)
    
    foo:showSum(3,3) -- 6
    foo:showProduct(3,3) -- 9
    

    That’s it.



    Now, as long as we’re here, let’s look at what your second function is doing:

         local val = self.__group[key]
         if type(val) == "function" then 
             self.__group:val()
             return function() end 
         end
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code class User attr_accessor :name end u = User.new u.name =
I have the following class written by someone else that I'm trying to understand
I have the following class which is intended to return the subject line of
I have the following class that enumerates all COM ports on the local PC,
Hello I have the following setup for a treeview: <local:BuddyManager x:Key=bmBuddyManager /> <CollectionViewSource x:Key=cvsBuddyManager
Say I have the following EJB (using ejb3): @Stateless(name=Queries) @Remote(Queries.class) @Local(Queries.class) public final class
I have the following code: class MyClass def method foo = MyClass.all end end
In a public static class I have the following local method: private static int
I have following class public class ButtonChange { private int _buttonState; public void SetButtonState(int
I have following class (as seen through reflector) public class W : IDisposable {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.