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

  • SEARCH
  • Home
  • 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 7739819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:37:01+00:00 2026-06-01T08:37:01+00:00

Say I declare a lua function using the : operator like so: function ClassName:myFunc(

  • 0

Say I declare a lua function using the : operator like so:

function ClassName:myFunc( stuff )
    --do stuff
end

And then say I store that function in a table like so:

someTable = {
    ClassName.myFunc,
    someGlobalFunc,
} 

And then, say I have another function that goes through the table and attempts to call the given functions.

function ClassName:callStuffInThisTable(table)
    -- I go through the table, which might be someTable above, and call all the functions
end

My question is, how do I know if a function in the table is owned by ClassName, so that I can call it using self?

  • 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-01T08:37:02+00:00Added an answer on June 1, 2026 at 8:37 am

    You don’t. At least, Lua isn’t going to tell you.

    function ClassName:myFunc( stuff ) is just syntactic sugar as far as Lua is concerned. It’s no different from this: ClassName.myFunc = function (self, stuff). The functions are equivalent.
    Likewise for the : call syntax, ClassName:myFunc(stuff) is semantically equivalent to ClassName.myFunc(ClassName, stuff).

    It is up to you to know what your functions are and what they do. This requires coding discipline. If you have a list of functions that you need to call in a loop, then they should be designed to be called with the same parameters.

    There are two ways to do this. One way is to make all of the functions “class functions”:

    someTable = {
        ClassName.myFunc,
        function(self, ...) return someGlobalFunc(...) end,
    } 
    

    This way, the self parameter is ignored. Obviously, you could create a special function table object that has functions for inserting “global” functions into the table that will automatically generate the wrapper:

    function insertFuncIntoTable(self, func)
      self[#self + 1] = function(self, ...) func(...) end
    end
    
    insertFuncIntoTable(someTable, someGlobalFunc)
    

    Note: there is a difference between these, assuming “someGlobalFunc” is actually a member of the global table (rather than a local). This version will take the value that _G["someGlobalFunc"] currently has, just as your original code does. However, the first version takes the value that it has at the time it is called, which may be a different function from the one it was at the time someTable was created.

    So this version is safer.

    Alternatively, you can make it so that any “class functions” in the table are explicitly bound to an object instance:

    someTable = {
        function(self, ...) ClassName.myFunc() end,
        function(self, ...) return someGlobalFunc(...) end,
    } 
    

    BTW, in general, if you declare a function using : syntax, you’re supposed to use that function that way, via instance:myFunc(...). Obviously it’s just a Lua function like any other, so you can do what you like. But misuse can make understanding what’s going on harder.

    Lua affords you a lot of power. But you’re still required to exercise judgement and discipline when coding. Lua will not save you from yourself (entirely).

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

Sidebar

Related Questions

Let's say I want to declare a function that has this java signature: public
I understand that when I declare a member function as const I actually say
say i have a nvarchar field in my database that looks like this 1,
Say I declare a header file with a variable: int count; Then in the
Lets say I declare a table variable with some columns in it. declare @MyContacts
I'm curious if I declare a column to be, say, varchar(5000) and then start
I'm using MooseX::Declare and methods, which uses MooseX::Method::Signatures. Let's say I have a class
Let's say I have the following simple table variable: declare @databases table ( DatabaseID
Let's say I have the following, declare @A table (a int) insert into @A
Is there any reason why one would declare a array final? say something like

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.