__lt, __le, __eq Support Removed?

I’m having a very basic problem here: I can’t seem to get the __lt, __le, or __eq metamethods to work in studio. Code that runs just fine, like the example below, on other interpreters fails on Roblox. I doubt this is a bug since it is a Lua feature probably used in lots of games, but I can’t seem to figure out what I’m doing wrong. I even reinstalled studio!

local mt = {}
function mt:__lt(other)
    return self[1] < other
end

print(setmetatable({1}, mt) < 3)
--> Workspace.Script:6: attempt to compare table with number

Any ideas what could be wrong?

It seems that it just can’t be compared with a number. Try comparing it with another table.

Huh, how interesting! I guess that is a slight difference from the vanilla interpreter. This code works fine:

local mt = {}
function mt:__lt(other)
	return self[1] < other[1]
end

local a = setmetatable({1}, mt)
local b = setmetatable({2}, mt)
print(a < b)

Thanks!

1 Like

"Unlike arithmetic metamethods, relational metamethods do not support mixed types. Their behavior for mixed types mimics the common behavior of these operators in Lua. If you try to compare a string with a number for order, Lua raises an error. Similarly, if you try to compare two objects with different metamethods for order, Lua raises an error.

An equality comparison never raises an error, but if two objects have different metamethods, the equality operation results in false, without even calling any metamethod. Again, this behavior mimics the common behavior of Lua, which always classifies strings as different from numbers, regardless of their values. Lua calls the equality metamethod only when the two objects being compared share this metamethod."-Programming in Lua : 13.2

3 Likes

Ah, the interpreter I was using since I haven’t installed Lua on my windows machine is found here, which apparently runs version 5.3.5. The spec must have changed somewhere between 5.0 (the version that manual was written for) and 5.3.5.