lua-users home
lua-l archive

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


On Mon, Oct 7, 2019 at 7:01 PM Oliver Kroth  wrote:
there are function returning a boolean value, which in case of failure
need to return a third, distinct value. nil.
Think of a function that for a file name returns whether it is a
directory or not.


In this situation you would better return
- filename (a string, the function's parameter) if it is a directory
- fail, 'Not a directory' if it is a file
- fail, err_mes if some error occurred

A nice one-liner:
set_current_dir(assert(is_a_directory(filename)))
This short one-liner would be impossible if the function is_a_directory(filename) returns true/false


I would like to stay with that a function on error returns something
that evaluates to nil, not false.
 
A similar thought:
Currently, string.match returns a tuple of captures which may be strings and numbers.
Theoretically, it was possible to add to Lua patterns a new kind of captures having boolean values.
After switching to "fail == false" this possibility will be closed forever.