lua-users home
lua-l archive

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


I missed a slew of post while writing my previous one, and it became redundant before I even submitted it.

Sorry for the noise.

-- Pierre-Yves


2010/12/15 Pierre-Yves Gérardy <pygy79@gmail.com>
On Wed, Dec 15, 2010 at 08:31, Dirk Laurie <dpl@sun.ac.za> wrote:
-- 'nil' makes no distinction between 'unknown' and 'non-existent'.

Besides `nil` which represents nothingness explicitly, Lua also has an implicit nothing. Try print(nil) versus print(print()), for example. In this context, functions with no return value evaluate to nothing at all, not even nil.

There's no warranty that `print( tostring( foo() ) .. "bar" )` will work all the time. You must use `tostring(( foo() ))` to be safe. 

It is only problematic two constructs, AFAICT: when passing functions arguments to C functions (not designed to perpetuate the illusion or the One True Nil) and when using varargs. 

    function nop () end
    function baz (...) print( ... ) end
    baz( nop() ) --> prints an empty line.

If you use an explicit variable, there's no problem:

    function qux (a) print( a ) end
    qux( nop() ) --> prints nil.

Surprisingly, some (most?) functions in the standard lib don't behave as if an empty arg list was nil...

I'll add this to the Gotchas thread as well, because, it puzzled me at first, and although I now understand how it works technically, it is still a leak in the language abstraction.

-- Pierre-Yves