lua-users home
lua-l archive

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


Dear Italo,
This behavior is called String Coercion.

Thanks, Edu, I'm aware. The thing is that this implicit coercion is a weakly typed language behavior for what I know. "Usually" not a good thing. See this example:

'5' * 5  # one could expect it to create a string with five 5 characters. In lua, it gives you 25

#define LUA_NOCVTS2N

So, I can compile lua to not allow the string + number operation? Is that it? Nice to know. Seems an interesting approach for embeded devices. Was that your use case?

Parke, I too believe a roadmap is a great way to align expectations. Knowing what is to come is very helpful on the long run. VERY!


2017-06-19 19:05 GMT-03:00 Edu Araújo <jorgeeduardoaoliveira@gmail.com>:
Em 19 de jun de 2017 18:50, "Italo Maia" <italo.maia@gmail.com> escreveu:
(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.

Dear Italo,
This behavior is called String Coercion.

Lua performs automatic conversion of numbers to strings and vice versa where it is appropriate. This is called coercion.
> = "This is Lua version " .. 5.1 .. " we are using."
This is Lua version 5.1 we are using.
> = "Pi = " .. math.pi
Pi = 3.1415926535898
> = "Pi = " .. 3.1415927
Pi = 3.1415927

Jorge Eduardo de A. Oliveira
Computer Science Undergraduate Student




--
"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
===========================