Log In  

The documentation says tonum(val) returns nil if the conversion fails, but it actually returns nothing. Tested in 0.1.11d.

P#46938 2017-12-02 13:03 ( Edited 2017-12-03 03:36)

Seems to work ok:

local r=tonum("sdfds")
if r==nil then
    print("r is nil")
else
    assert("should not be there")
end
P#46939 2017-12-02 13:46 ( Edited 2017-12-02 18:46)

r is set to nil even when the function does not return anything. Instead, try the following:

print(type(nil))
print(type(tonum("sdfds")))
P#46943 2017-12-02 14:35 ( Edited 2017-12-02 19:35)

Agreed that either the docs or the function should be fixed. Lua does treat "no return value" and "returns nil" as distinct. Official Lua's tonumber() returns nil.

print(tostr(nil))           -- "[nil]"
print(tostr(tonum('xyz'))   -- "[no value]"
P#46948 2017-12-02 22:36 ( Edited 2017-12-03 03:42)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-28 14:46:05 | 0.006s | Q:15