Compile Lua without automatic conversion between strings and numbers

Lua is usually a strongly typed language , with little or no implicit conversion between data types.

However, numbers and strings do get automatically forced in several cases :

Lua provides automatic conversion of string and number values ​​at runtime. Any arithmetic operation applied to a string tries to convert this string to a number, following the rules of Lua lexer. (A string can have leading and trailing spaces and a character.) Conversely, whenever a number is used where a string is expected, the number is converted to a string in a reasonable format

Thus:

local x,y,z = "3","8","11" print(x+y,z) --> 11 11 print(x+y==z) --> false print(x>z) --> true 

I do not want it. How to recompile Lua interpreter to remove all automatic conversion?

I would prefer:

 print(x+y) --> error: attempt to perform arithmetic on a string value print(x>1) --> error: attempt to compare number with string print(x..1) --> error: attempt to concatenate a number value 
+8
c lua strong-typing
source share
1 answer

The illustrious LHF commented above that this is not possible out of the box, and requires editing the internal Lua properties, starting at http://www.lua.org/source/5.2/lvm.c.html#luaV_tonumber

Mark this as an answer to close this question. If someone later decides to give a detailed answer about what needs to be done, I will gladly switch the acceptance mark to this answer.

+2
source share

All Articles