lua-users home
lua-l archive

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


> Not exactly.  The function io.open, for instance, does not return a
> "true", just the actual file handler on success.  On a failure, it
> returns nil followed by an error message.  IIRC, the two functions
> that work the way you described are pcall and xpcall, since both are
> very general functions.

Indeed, several functions return useful values in case of success.
Nevertheless, they overload the first result as a value that signals
success or failure, too. 'io.open' on success does not return true, but
it returns a truly value.

With hindsight, I sometimes regret that when we introduced booleans in
Lua, decades ago, we did not change the standard error return "nil, msg"
to "false, msg". If nil is some kind of "absent value", it is weird that
"msg" comes after something that is not there.

So, the motivation for the change is to move our minds from the 
concept that errors return (nil,msg) to the concept that errors return
(falsy,msg). Maybe one day in the distant future we might change
that falsy to false instead of nil.

As a killing argument for the change in style, "if not x then" is
more efficient than "if x == nil then", so everybody should be using 
that by now  :-)

There are zero ("zero" as "no chance") of a new falsy value in Lua.

-- Roberto