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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:44:04+00:00 2026-05-22T12:44:04+00:00

I’m trying, as an exercise, to make a set implementation in Lua. Specifically I

  • 0

I’m trying, as an exercise, to make a set implementation in Lua. Specifically I want to take the simplistic set implementation of Pil2 11.5 and grow it up to include the ability to insert values, delete values, etc.

Now the obvious way to do this (and the way that works) is this:

Set = {}
function Set.new(l)
    local s = {}
    for _, v in ipairs(l) do
        s[v] = true
    end
    return s
end
function Set.insert(s, v)
    s[v] = true
end

ts = Set.new {1,2,3,4,5}
Set.insert(ts, 5)
Set.insert(ts, 6)

for k in pairs(ts) do
    print(k)
end

As expected I get the numbers 1 through 6 printed out. But those calls to Set.insert(s, value) are really rather ugly. I’d much rather be able to call something like ts:insert(value).

My first attempt at a solution to this looked like this:

Set = {}
function Set.new(l)
    local s = {
        insert = function(t, v)
            t[v] = true
        end
    }
    for _, v in ipairs(l) do
        s[v] = true
    end
    return s
end

ts = Set.new {1,2,3,4,5}
ts:insert(5)
ts:insert(6)

for k in pairs(ts) do
    print(k)
end

This works mostly fine until you see what comes out of it:

1
2
3
4
5
6
insert

Very obviously the insert function, which is a member of the set table, is being displayed. Not only is this even uglier than the original Set.insert(s, v) problem, it’s also prone to some serious trouble (like what happens if “insert” is a valid key someone is trying to enter?). It’s time to hit the books again. What happens if I try this instead?:

Set = {}
function Set.new(l)
    local s = {}
    setmetatable(s, {__call = Set.call})
    for _, v in ipairs(l) do
        s[v] = true
    end
    return s
end
function Set.call(f)
    return Set[f]
end
function Set.insert(t, v)
    t[v] = true
end

ts = Set.new {1,2,3,4,5}
ts:insert(5)
ts:insert(6)

for k in pairs(ts) do
    print(k)
end

Now the way I’m reading this code is:

  • When I call ts:insert(5), the fact that insert doesn’t exist to be called means that the ts metatable is going to be searched for "__call".
  • The ts metatable’s "__call" key returns Set.call.
  • Now Set.call is called with the name insert which causes it to return the Set.insert function.
  • Set.insert(ts, 5) is called.

What’s really happening is this:

lua: xasm.lua:26: attempt to call method 'insert' (a nil value)
stack traceback:
        xasm.lua:26: in main chunk
        [C]: ?

And at this point I’m stumped. I have absolutely no idea where to go from here. I hacked around for an hour with varying degrees of increasingly desperate variations on this code but the end result is that I have nothing that works. What undoubtedly obvious thing am I overlooking at this point?

  • 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-05-22T12:44:04+00:00Added an answer on May 22, 2026 at 12:44 pm

    Now the way I’m reading this code is:

    • When I call ts:insert(5), the fact that insert doesn’t exist to be called means that the ts metatable is going to be searched for “__call”.

    There’s your problem. The __call metamethod is consulted when the table itself is called (ie, as a function):

    local ts = {}
    local mt = {}
    
    function mt.__call(...)
        print("Table called!", ...)
    end
    
    setmetatable(ts, mt)
    
    ts() --> prints "Table called!"
    ts(5) --> prints "Table called!" and 5
    ts"String construct-call" --> prints "Table called!" and "String construct-call"
    

    Object-oriented colon-calls in Lua such as this:

    ts:insert(5)
    

    are merely syntactic sugar for

    ts.insert(ts,5)
    

    which is itself syntactic sugar for

    ts["insert"](ts,5)
    

    As such, the action that is being taken on ts is not a call, but an index (the result of ts["insert"] being what is called), which is governed by the __index metamethod.

    The __index metamethod can be a table for the simple case where you want indexing to “fall back” to another table (note that it is the value of the __index key in the metatable that gets indexed and not the metatable itself):

    local fallback = {example = 5}
    local mt = {__index = fallback}
    local ts = setmetatable({}, mt)
    print(ts.example) --> prints 5
    

    The __index metamethod as a function works similarly to the signature you expected with Set.call, except that it passes the table being indexed before the key:

    local ff = {}
    local mt = {}
    
    function ff.example(...)
      print("Example called!",...)
    end
    
    function mt.__index(s,k)
      print("Indexing table named:", s.name)
      return ff[k]
    end
    
    local ts = {name = "Bob"}
    setmetatable(ts, mt)
    ts.example(5) --> prints "Indexing table named:" and "Bob",
                  --> then on the next line "Example called!" and 5
    

    For more information on metatables, consult the manual.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites 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.