Comparisons

Issue #4 resolved
mezzodrinker created an issue

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)

  1. Chimaine

    Strings are automatically coerced to numbers in such cases. But something seems to be wrong. Will look into it.

  2. Chimaine

    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.

  3. Log in to comment