lua-users home
lua-l archive

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

I find this idea very interesting... i too write lot of code to workaround 
unexpected nils.

I would recommend using the ? before the variable like:
	x = ?expr.?field
with the option to define the ? operator behavior in meta-table. (useful for 
debugging or other neat stuff)

so you can do:
	if ?expr.?field.?field2 then end
instead of:
	if expr and expr.field and expr.field.field2 then end

On the other hand I don't want lua to become perl .... 


On Monday 28 June 2004 08: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 ?[].
> 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