lua-users home
lua-l archive

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


This seems like an entire extra dimension of complexity, if we do it
right. What happens if you pass a question-marked nil into setfenv? If
you use it to create a coroutine? The "and" idiom, while
wacky-looking, is at least a simple and easily understood method of
doing this.

On Sun, 27 Jun 2004 23:42:14 -0700, Mark Hamburg <mhamburg@adobe.com> 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 ?[].
> 
> 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
> 
>