Lua 5.3 overriding the ~ (__bnot) operator for integer values using debug.setmetatable

Viewed 105

This example

debug.setmetatable(0,
{
    __bnot = function(a) -- ~ operator
        print("yay ".. a)
        return a
    end
})
print(~ (0))
print(~ (0.3))
print(~ (1))
print(~ (2))
print(~ (2.1))

Outputs the following

-1
yay 0.3
0.3
-2
-3
yay 2.1
2.1

I was expecting

yay 0
0
yay 0.3
0.3
yay 1
1
yay 2
2
yay 2.1
2.1

It looks like it is not called for integer values.

How do I override the not operator for all numbers?

1 Answers

Per the comments: You can not override everything with a metamethod. Metamethods are fallbacks in Lua. You can only implement operations which were not defined in the Lua language. For example, bitwise operations on numbers having nonzero fractional part were not defined.

Related