Consolidating Lua Custom Numbers

I found out about metatables in Lua, and I wanted to implement range operators like in Ruby, so I used this model

debug.setmetatable(1, {
    __concat = function(a, b) 
        if a > b then
            error(table.concat({
                "attempt to create a range of values with a",
                "minimum larger than the maximum"
            }, " "))
        elseif a == b then
            return a
        else
            return unpack((function(nStart,nEnd)
                local nTable = {}
                for it = nStart,nEnd do
                    table.insert(nTable, it)
                end
                return nTable
            end)(a, b))
        end
    end
})

print(6 .. 6) 

But it looks like it continues to use the default behavior. Is there any way to make this work? I know that I could create a function to emulate behavior and call it a range (n, n2) or similar, but that defeats the goal. Thanks.

+4
source share
1 answer

See section 3.4.5 of the Lua 5.2 manual.

Lua ('..'). , , . 3.4. __concat (. . 2.4).

, lvm.c, luaV_concat.

+4

All Articles