ruby picture

RCR 192: New variable declaration syntax in blocks

Submitted by grenzi (Wed Jan 14 07:01:07 UTC 2004)

Abstract

this rcr suggest the removal of a "|" to shorten variable declarations

Problem

e block declaration like { |a|
 ..
} requires to enclose the variable beetween a couple of "|". We may just use a single one, as in SmallTalk or Groovy

Proposal

change this syntax from
 do |a,b,c|
  ..
 end
to
 do a,b,c |
  ..
 end

Analysis

This is just a little change but may break something.
but I may be wrong.
The proposed change reduces line noise and
saves one keystroke. Not a big saving, but better than actual syntax.

you would just need:

ary.map {x| x.succ} 
instead of
ary.map {|x| x.succ}

Possibly we could introduce the change as a parallel syntax, leaving both available. Warning:
This may cause ambiguity in cases like this


a=0; b=1 proc { a | b }
is it 'a' a formal argument or a reference to an external variable BitOR'ed with b ?
With the new syntax you may have to write like this:

a=0;b=1 proc {| a|b }
not a big deal for really rare cases.

If both syntax are available this may be hard to check. We could be forced to write

proc { || a|b}
 for this case

Implementation

none ATM
ruby picture
Comments Current voting

The ambiguities introduced into this grammar make it a very undesirable change, IMO. The initial pipe makes the grammar easier to parse, and easier to read--and I think ease of readability should come before ease of writability. Saving a single keystroke is not worth the added complexity and potential visual ambiguities.

- Jamis Buck


Thanks, James. I feel same way. Actually, in the pre-relase phase (back in 1993), the block parameter syntax was exactly what you described here. I changed the syntax for the particular reason which James stated above.

-matz.


I did not mind about parsing efficiency cause we're going to have a bytecompiled form for our script sometime in the future. But It seem I'm the only one that finds the proposed new syntax more clear than the old. Ok, let's kill this rcr :)

- grenzi


Strongly opposed 6
Opposed 1
Neutral 0
In favor 0
Strongly advocate 0
ruby picture
If you have registered at RCRchive, you may now sign in below. If you have not registered, you may sign up for a username and password. Registering enables you to submit new RCRs, and vote and leave comments on existing RCRs.
Your username:
Your password:

ruby picture

Powered by .