lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On Mon, Jun 19, 2017 at 4:49 PM, Italo Maia <> wrote:
> (1) '5' + 5 gives you an 10.0 in the interactive console. Isn't this a
> weakly typed language behavior?
> (2) '5' + 5 > 10.0, string plus integer equals float. If an error was throw
> here, it would be easier to understand than the given behavior.

(1) In this case, it's a strongly-typed behavior. It's not treating
strings and numbers as interchangeable; it's an explicitly documented
behavior of the + operator, and there's a separate concatenation
operator that has different semantics (number .. number -> string).
This isn't like PHP where strings that look like numbers effectively
ARE numbers and all sorts of bizarre things can happen with them. It's
more like how C++ tries to find a type conversion to a function that
doesn't directly support the types you used.

(2) No, string plus number equals number, which can compare to another
number. Lua does not have a distinct integer type; it didn't have an
integer type _at all_ until 5.3 and that's a subtype with well-defined
conversion rules (it coerces to a floating-point number very readily).

/s/ Adam