lua-users home
lua-l archive

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


Would it be useful to define syntactic sugar that would allow nils to turn
into no-ops? For example, what if:

    x = expr?.field

Were sugar for:

    local var = expr;
    x = expr and expr.field

In other words, ?. applied to nil yields nil. Similarly for ?[].

func?() could result in a call to a function returning nothing if func was
nil. (I leave it as an open point of discussion whether any arguments get
evaluated. They probably do.)

obj?:message() would be like a call to a function returning nothing if obj
was nil. Similarly, for obj:message?() if obj fails to support message. If
both are optional, then one writes: obj?:message?().

I find myself writing a fair amount of code that avoids going down a path
when something is nil and if this is a common pattern for other people it
might be useful to encode it in some form of syntactic sugar.

Mark