(1) it's an explicitly documented behavior of the + operator
> 10.0 == 10true> tostring(5+'5') == tostring(5+5)false> 5 .. '5'
>> 5 .. 5
55> tostring(5..'5') == tostring(5..5)true
25Well, the examples above leave some room for thought.
2017-06-19 19:16 GMT-03:00 Coda Highland <firstname.lastname@example.org>:On Mon, Jun 19, 2017 at 4:49 PM, Italo Maia <email@example.com> 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).
--"A arrogância é a arma dos fracos."
Me. Italo Moreira Campelo MaiaCo-fundador do Grupo de Usuários Python do CearáSecretário ForHacker (fb.com/ForHackerSpace)