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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:14:02+00:00 2026-06-13T01:14:02+00:00

While reading Programming in Lua , I tried this example given in the book

  • 0

While reading Programming in Lua, I tried this example given in the book for operator overloading

Set = {}

mt = {}
mt.__add = Set.union

--create a new set with the values of the given list
function Set.new (l) 
    local set = {}
    setmetatable (set, mt)
    for _, v in ipairs (l) do 
        set [v] = true
    end
    return set
end 

function Set.union (a, b)
    local result = Set.new {}
    for k in pairs (a) do result [k] = true end
    for k in pairs (b) do result [k] = true end
    return result
end

function Set.intersection (a, b)
    local result = Set.new {}
    for k in pairs (a) do result [k] = b[k] end
    return result
end

function Set.tostring (set)
    local l = {}
    for e in pairs (set) do
        l[#l + 1] = e
    end
    return "{" .. table.concat (l, ", ") .. "}"
end 

function Set.print (s)
    print (Set.tostring (s))
end 

s1 = Set.new {10, 20, 30, 50}
s2 = Set.new {30, 1}

Set.print (s1)
Set.print (s2)

s3 = s1 + s2
Set.print (s3)

But with the latest lua for windows I am getting the following error

lua: C:\meta.lua:47: attempt to perform arithmetic on global 's1' (a table value)
stack traceback:
    C:\meta.lua:47: in main chunk
    [C]: ?
{30, 10, 20, 50}
{1, 30}
  • 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-13T01:14:03+00:00Added an answer on June 13, 2026 at 1:14 am

    You are making this assignment too early:

    mt.__add = Set.union
    

    because Set.union is not initialized yet.

    Move this below Set.union and it will work.

    For the same reason, if you assign mt.__mul, this should be below Set.intersection

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

Sidebar

Related Questions

While reading the book Programming Ruby , one example shows how blocks can be
While reading Programming Ruby , I ran across this code snippet: while gets num1,
So, I'm reading this book: C++ GUI Programming with Qt 4, Second Edition by
I am fairly new to programming and while doing a lot of reading this
I'm reading this book: The C Programming Language - By Kernighan and Ritchie (second
While I was reading the book - O'Reilly Java Servlet Programming. There was a
Recently, while reading a Socket Programming HOWTO the following section jumped out at me:
I am very new to programming in general, while reading about PHP I saw
While reading this class BitmapFactory I noticed that almost all methods inside are static.
While reading the answers to this question I got a doubt regarding the default

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.