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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:20:31+00:00 2026-05-14T23:20:31+00:00

I’ve found some places on the web saying that operators in Lua are overloadable

  • 0

I’ve found some places on the web saying that operators in Lua are overloadable but I can’t seem to find any example.

Can someone provide an example of, say, overloading the + operator to work like the .. operator works for string concatenation?

EDIT 1: to Alexander Gladysh and RBerteig:

If operator overloading only works when both operands are the same type and changing this behavior wouldn’t be easy, then how come the following code works? (I don’t mean any offense, I just started learning this language):

printf = function(fmt, ...)
    io.write(string.format(fmt, ...))
end

Set = {}
Set.mt = {}    -- metatable for sets

function Set.new (t)
    local set = {}
    setmetatable(set, Set.mt)
    for _, l in ipairs(t) do set[l] = true end
    return set
end


function Set.union (a,b)
    -- THIS IS THE PART THAT MANAGES OPERATOR OVERLOADING WITH OPERANDS OF DIFFERENT TYPES
    -- if user built new set using: new_set = some_set + some_number
    if type(a) == "table" and type(b) == "number" then
        print("building set...")
        local mixedset = Set.new{}
        for k in pairs(a) do mixedset[k] = true end
        mixedset[b] = true
        return mixedset
    -- elseif user built new set using: new_set = some_number + some_set
    elseif type(b) == "table" and type(a) == "number" then
        print("building set...")
        local mixedset = Set.new{}
        for k in pairs(b) do mixedset[k] = true end
        mixedset[a] = true
        return mixedset
    end

    if getmetatable(a) ~= Set.mt or
        getmetatable(b) ~= Set.mt then
        error("attempt to 'add' a set with a non-set value that is also not a number", 2)
    end

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


function Set.tostring (set)
    local s = "{"
    local sep = ""
    for e in pairs(set) do
        s = s .. sep .. e
        sep = ", "
    end
    return s .. "}"
end

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

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

Set.mt.__add = Set.union

-- now try to make a new set by unioning a set plus a number:
s3 = s1 + 8
Set.print(s3)  --> {1, 10, 20, 30, 50}
  • 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-14T23:20:31+00:00Added an answer on May 14, 2026 at 11:20 pm

    The metatable function only works on tables, but you can use debug.metatable to set the strings metatable…

    > mt = {}
    > debug.setmetatable("",mt)
    > mt.__add = function (op1, op2) return op1 .. op2 end
    > ="foo"+"bar"
    foobar
    > 
    

    Another approach is to use debug.getmetatable to augment the built-in string metatable (answering the question in the comment below):

    ~ e$ lua
    Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
    > debug.getmetatable("").__add = function (op1, op2) return op1 .. op2 end
    > ="foo"+"bar"
    foobar
    > 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 401k
  • Answers 401k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer A lot of problems with printing you can solve with… May 15, 2026 at 4:34 am
  • Editorial Team
    Editorial Team added an answer This may be Windows caching the icons. From http://www.tweakxp.com/article37003.aspx regarding… May 15, 2026 at 4:34 am
  • Editorial Team
    Editorial Team added an answer BTW Windows Server AppFabric is out of beta. That's what… May 15, 2026 at 4:34 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.