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 8052303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:30:42+00:00 2026-06-05T07:30:42+00:00

What is the difference between tables and metatables in Corona? What are the types

  • 0

What is the difference between tables and metatables in Corona? What are the types of metatables? How and where can I use them? What is the main purpose of using tables and metatables?

  • 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-05T07:30:43+00:00Added an answer on June 5, 2026 at 7:30 am

    Lua (which is the language that Corona is based on) uses metatables for different purposes.

    The relevant entry in the manual is Section 2.8.
    A nice tutorial can be found here or here.

    A metatable is just a table like any other, but is set as metatable on another table (which I’ll call a base table further on, to make a difference between the 2 tables).

    A metatable can contain anything, but the special keys (starting with a double underscore) are the interesting ones. The values set to this keys in this table will be called on special occasions. Which occasion depends on which key. The most interesting are:

    • __index: Will be used whenever a key in the base table is looked up, but does not exist. This can either contain table, in which the key will be looked up instead, or a function, which will be passed the original table and the key. This can be used for implementing methods on tables (OOP style), for redirection, fall through cases, setting defaults, etc etc
    • __newindex: Will be used whenever a new key is to be assigned in a table (which was previously nil). If it’s a table, the key will be assigned in that table. If it’s a function, that function will be passed the original table, key and value. This can be used for controlling access to a table, preprocessing data, redirection of assignments.
    • __call: enables you to set a function to be called if you use eg. table().
    • __add,__sub,__mul,__div,__mod are used to implement binary operations,
    • __unm is used to implement unary operations,
    • __concat is used for implementing concatenation (the .. operator)
    • __len is used for implementing the length operator (#)
    • __eq,__lt,__le are used for implementing comparisons

    A small thing to know when using __index & co.: in those methods, you should use rawget and rawset in order to prevent calling the metamethod each time again, causing a loop.
    As a small example:

    t={1,2,3}  -- basetable
    mt={} -- metatable
    mt.__index=function(t,k)
        print("__index event from "..tostring(t).." key "..k)
        return "currently unavailable"
    end
    mt.__newindex=function(t,k,v)
        print("__newindex event from "..tostring(t).." key: "..k.." value: "..v)
        if type(k)=="string" then
            rawset(t,k,v:reverse())
        else
            rawset(t,k,v)
        end
    end
    mt.__call=function(t,...)
        print("call to table "..tostring(t).." with arguments: ".. table.concat({...},','))
        print("All elements of the table:")
        for k,v in pairs(t) do print(k,v) end
    end
    setmetatable(t,mt)
    
    t[4]="foo" -- this will run the __newindex method
    print(t[5]) -- this will run the __index method
    t("foo","bar")
    -- Multiple fall through example:
    t={}
    mt={}
    mt2={}
    setmetatable(t,mt)  -- metatable on base table
    setmetatable(mt,mt2) -- second layer of metatable
    mt.__index=function(t,k) print('key '..k..' not found in '..namelookup[t]) return getmetatable(t)[k] end -- tries looking nonexistant indexes up in mt.
    mt2.__index=mt.__index -- function was written portably, reuse it.
    
    t[1]='A'
    mt[2]='B'
    mt2[3]='C'
    namelookup={[t]="t",[mt]="mt",[mt2]="mt2"}
    print(t[1],t[2],t[3],t[4])
    

    Now these are but silly examples, you can do much more complex stuff. Take a look at the examples, take a look at the relevant chapters in Programming in Lua, and experiment. And try not to get confused 😉

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

Sidebar

Related Questions

Can anyone break it down in plain English the performance difference between using temp
whats the difference between the two types of temp tables @tmp vs #tmp in
I have to admin there is slight difference between the columns from both tables.
What is main difference between INSERT INTO table VALUES .. and INSERT INTO table
What is the difference between linking two tables and then the PK is an
What is the difference between local and global temporary tables in SQL Server?
Assuming the proper tables are in place - what is the difference between the
Is there a measurable performance difference between using INT vs. VARCHAR as a primary
How can I tell the difference between a table not being present in the
When joining two tables, what are the difference between the two blocks below and

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.