Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator overloading not working

Tags:

lua

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}
like image 547
Apeirogon Prime Avatar asked Feb 18 '23 20:02

Apeirogon Prime


2 Answers

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

like image 183
finnw Avatar answered Feb 23 '23 01:02

finnw


You need to define mt as a suitable metatable:

mt = { __add = Set.union, __mul = Set.intersection, __tostring = Set.tostring }
like image 40
lhf Avatar answered Feb 23 '23 00:02

lhf