lua-users home
lua-l archive

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


On Monday 28 June 2004 09:42, Mark Hamburg wrote:
> 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 ?[].

So why the extra syntax? if expr evaluates to nil then you're going to get an 
error which is hardly desirable, so the proposed behavior could be the 
default. Although I can't say I support it. The '.' operator for example is 
the indexing operator. Indexing a nil value is undefined so you would expect 
it to cause an error. I'm not in favor of unexpected behavior (e.g. not 
getting an error) although I can see it can help out. Same goes for all the 
other cases. Maybe we could use some mechanism for selectively ignoring 
errors though.

-- Dimitris