lua-users home
lua-l archive

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


(1) it's an explicitly documented behavior of the + operator

I'm unsure if documenting it makes it "ok" to use and still be consider it strongly typed. _javascript_ would kind of splash close in that pool. But the string concatenation operator does add some spice to the mix.

> -- lua 5.3
> 5+'5'
10.0
> 5+5
10
> 10.0 == 10
true
> tostring(5+'5') == tostring(5+5)
false
> 5 .. '5'
55
>> 5 .. 5
55
> tostring(5..'5') == tostring(5..5)
true
> 5..5
error
> '5'..'5'
55
> 5*5
25
> '5'*'5'
25

Well, the examples above leave some room for thought.


2017-06-19 19:16 GMT-03:00 Coda Highland <chighland@gmail.com>:
On Mon, Jun 19, 2017 at 4:49 PM, Italo Maia <italo.maia@gmail.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).

/s/ Adam




--
"A arrogância é a arma dos fracos."

===========================
Me. Italo Moreira Campelo Maia
Co-fundador do Grupo de Usuários Python do Ceará
Secretário ForHacker (fb.com/ForHackerSpace)
Desenvolvedor Full-Stack, Escritor, Empresário, Visionário
-----------------------------------------------------
Meu Livro, Site, Blog
===========================