- edited description
Comparisons
Assuming the user tries one of the following statements...
string < number;
string > number;
string <= number;
string >= number;
... - or any other comparison that doesn't contain either two strings or two numbers - the return value of <
and >
will always be false
while <=
and >=
will always result in true
. I think the comparison of a number and a string should either result in a runtime error or in the comparison of
number < #string;
or whatever comparator is used.
The Lua Reference Manual said that the code of the lt operator looks like this:
function lt_event (op1, op2)
if type(op1) == "number" and type(op2) == "number" then
return op1 < op2 -- numeric comparison
elseif type(op1) == "string" and type(op2) == "string" then
return op1 < op2 -- lexicographic comparison
else
local h = getbinhandler(op1, op2, "__lt")
if h then
return not not h(op1, op2)
else
error(···)
end
end
end
Comments (6)
-
reporter -
reporter - changed title to Comparisons
- edited description
-
Strings are automatically coerced to numbers in such cases. But something seems to be wrong. Will look into it.
-
- changed status to resolved
Change meta method system and implementations of serveral related functions. Fixes
#4.→ <<cset 7a0915682873>>
-
Actually, what I said about coercion is wrong. Automatic coercion only happens on arithmetic operations (not counting library functions), but not on relational operations. Even 1 == "1" yields false. I'm undecided if I want to keep it that way or deviate from the Lua specification.
-
- removed version
Removing version: 2.0 (automated comment)
- Log in to comment