[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Syntactic sugar idea: Safe use of nil
- From: Jarrod Roberson <jarrod@...>
- Date: Mon, 28 Jun 2004 12:00:31 -0400
Mark Hamburg wrote:
why does everyone want to make every readable easy to maintain language
as hard to read as C/C++?
are you that lazy, scripting languages, as a matter of fact ALL
languages should be as EASY to READ and MAINTAIN
as possible adding different ways to do something really simple makes
the language hard to learn for beginners and hard
to maintain becuase of all the different idioms because of subtle
differences and side effects.
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.
this is a BAD idea.