Greg Fodor wrote:
Maybe you could do:
return v + 1 if its < 10
-- snip
return v + 1 if < 10
Unfortunately this seems much more difficult and ambiguous
for a number of reasons. First, it is impossible to know
that our implied "it" should be "v + 1" without making
assumptions about the intent. (Why don't we include the
result of the return expression itself? What if it's more
complex?)More importantly, semantics of order of evaluation become
confusing. Despite v + 1 being evaluated during the
if conditional, it syntactically appears in the return
statement. My approach preserves all current ruby semantics
and is more intentional, I feel. It basically transforms
this:tmp = m(x)
h(tmp) if g(tmp)into just
h(it) if g(it = m(x))
The it keyword is essential for one reason and nice for
a few others. It is essential because of the ruby semantics
for determining identifiers vs. methods, referenced in my
original post.It is nice because we no longer have to come up with the
name "tmp,"ruby provides a consistent intentional name.
It is also nice because it is less code. It is also nice
because it provides correct scope semantics so it will be
GCed at the optimal time.No need for it to be such a special built-in construct.
Depends on what you mean by "need." For an optimal solution,
I feel it does. "Optimal" means:- It only evaluates m(x) once (yours does this)
- It is the most terse representation of intent (subjective)
- It introduces the correct scope (yours does not)
- It releases memory upon GC after the statement (yours doesn't)
- It avoids side effects (yours changes global space)Its hard to think of a solution to this implemented in ruby
that fits these parameters without introducing a new
language level construct.
How about something like: return $1 if (v + 1) < 10 if you wanted to return v + 1? If you wanted to return v you could use: return $2 if ((v) +1) < 10. Improvements would be using some of the regular expression flags to control what $1, $2, etc. refer to.