[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Syntactic sugar idea: Safe use of nil
- From: Dimitris Papavasiliou <jimmyp@...>
- Date: Mon, 28 Jun 2004 14:34:16 +0300
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