Greg Fodor wrote:
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.I like this better than def it() since it seems more intentional,
but it too results in side effects into global space and prevents
GCing early.Another problem is that you run into problems mixing the
meaning of parenthesis, since within a given expression in your
example you may not want to set aside storage space for all
parenthesized expressions, despite the fact you may need to enforce
order of operations.For example:
puts $2 if (x + y) * (a - b) < 10
will copy the result of x + y in RAM even if your parentheses are
there to simply make the add happen first. Having it be something
new in this context like pipes separates concerns and avoids these
technical consequences.
That was why I had put in the comment about using flags from regular expressions to control whether something was added to the global variables. I am still reading about regular expressions and don't remember the flags in question. This would also prevent extraneous data being placed in the global variables which was another of your concerns.
FYI, this would be:
puts it if (x + y) * |(a - b)| < 10
My problem, and probably that of a lot of other people, is that it is almost impossible to scan over a line like that and figure out what is going on. If I am trying to debug a program I want the code to be as clear as possible. I can see right away that something is placed on the screen if the expression evaluates to < 10, but what? Now I have to stop and try and remember how this is evaluated. Is it the information in the first set of parentheses or between the pipes or something else? This break in concentration can be a real killer in finding obscure bugs.
My proposal also suffers from some of this problem but as it is "inherited" from regular expressions it may be easier to figure out.
···
We just get temp storage for a - b and preserve a single meaning
of parentheses.