lua-users home
lua-l archive

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

Op Wo. 20 Feb. 2019 om 23:44 het Egor Skriptunoff
<> geskryf:

> This is "Nitpicking on Wednesdays".

> 1)
> Lua 5.4 (as of 2019-Jan-30 on Github)

If is only on Github, not in the latest work version, it does not
belong on lua-l, not even in NoW. There is a Github "issue" mechanism
that should be used.

> 5)
> The S2N-coercion (from string to number) is somewhat anarchic:
>    local x = "2" | 3   -- coercion fails
>    local x = "2" + 3   -- coercion results in integer value
>    for x = "2", 3 do   -- coercion results in float value
> The coercion logic is unexpected and non-obvious.
> Even disabling of auto-S2N-coercion would be a lesser surprise  :-)
> The coercion creates an illusion that in Lua you could use strings instead of numbers.
> But it's difficult to describe when you can rely on such coercion and when you can not.
> S2N-coercion isn't consistent across all the numeric operations:
>    "10" - "9" > 0             -- true
>    "10" > "9"                 -- false
>    math.max("10", "9")=="10"  -- false
> S2N-coercion is full of traps, using it is considered a bad practice, and it is becoming more complicated.
> Is it the right moment to remove auto-S2N-coercion from Lua?

Actually, auto-S2N coercion _has been removed_ from the current work
version of Lua 5.4.

All that remains is a sample set of metamethods. Your first two
examples invoke the __bor and __add metamethods. In the 'for'
statement, granted, it still is just old-fashioned coercion. Maybe we
could have a __tonumber metamethod too, to give control in that

If the metamethod does not behave the way you expect or want, change
it.  You don't even need the debug library for that: the metatable for
strings already exists. Just assign nil to _bor and __add etc, and
even the simulation of S2N will be disabled.

For example, it would be straightforward to replace the supplied
metamethods by wrapping a library supporting arbitrary-length
integers. Or to have a Rebol-like ability to recognize many kinds of
literals and manipulate tem sensibly. e.g. make sens of expressions
   "£3/6/8" * 3

-- Dirk