lua-users home
lua-l archive

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


Another feature I'd really like to see is simplified support for possibly
nil values in expressions.

I frequently run across code in Lightroom where people end up
double-evaluating things to do a nil test because it's easier than using a
temporary; e.g.,

    t.foo and t.foo.baz

(This also has trouble if t.foo is false, but that's a different matter.)

One could write:

    ( t.foo or { } ).baz

But that's rather unnatural and it constructs a table in the nil case
(relatively expensive). We can get rid of the table construction with:

    ( t.foo or kEmptyTable ).baz

But now kEmptyTable better be a local or we've got a global lookup tacked
in.

The efficient way to write this is:

    local temp = t.foo

    temp and temp.baz

(Though that still ignores the issue with "false".)

What I'd like to see is support for something like:

    t.foo?.baz

?. is defined to return nil if it's left side is nil.

To go with this we also have:

?[] which similarly returns nil.

?: which evaluates the method parameters but then results in nothing if the
left side is nil

?() which evaluates the function parameters but then results in nothing if
the left side is nil

And similarly for string and table driven function calls.

Basically, the idea is to make it easy to support optional or missing values
as no-ops.

Mark