From: Jean-Hugues ROBERT Date: 2002-04-16T07:19:29+09:00 Subject: RCR: xx: yy as shorter notation for :xx => yy Hello, I am considering making a RCR but first I would like to make sure it has some value. Here it is: I would like xxx: yyy to be equivalent to :xxx => yyy. Example A: Call with named parameters. Instead of obj.do_something :some_arg => value1, :some_other_arg => value 2 you would write: obj.do_something some_arg: value1, some_other_arg: value2 Example B: Hash literal. Instead of a_hash = { :if => "page 4", :return => "page 5" } you would write a_hash = { if: "page 4" return: "page 5" } This syntax form is similar (with a whole different meaning) to the C construct where, instead of (*xxx).field (in Ruby: :xxx => yyy) you can write xxx->field (in Ruby?: xxx: yyy) I don't know if it should be restricted to symbol (i.e. must xxx be a symbol in xxx: yyy) or can it be extended to any expressions (i.e. (an_expression): yyy). Of course, this later one makes ":" become an operator, instead of a syntax sugar. And this has more impacts I guess (i.e. more work) on the Ruby parser. Rationnal: 1) xxx: yyy is shorter than :xxx => yyy 2) Promote the usage of symbols (efficient) 3) Compares well to similar constructs in some other languages that have either "named arguments" or "named values" (or both) 4) Natural, xxx: yyy is the syntax used in many internet protocols where the name of an attribute/field/element is followed by ":" and then the value of that attribute/field/element. Difficulties: I don't know for sure but there might be some conflicts with the "?:" ternary operator. Example: (a ? b:c) and (a ? b:c : d:e). However in this case I don't think that => would be considered valid either: (a ? :b => c : :d => e) but I may be wrong. I don't know for sure what can follow ":" in :xxx today. Ideal any xxx that is valid in :xxx should be ok in the xxx: form too. Implementation: The implementation requires some look ahead (at lexical level), not much probably. i.e. What may first appear has a reference to a local variable xxx or a function xxx turns out to be the xxx symbol (when ":" is encountered). When that happens, "=>" should be pushed back in the input stream (that later one is easy I guess). The implementation could focus on the simple cases first, when xxx is an identifier, and address more complex cases (when xxx is something more complex then an identifier, an arbitrary expression) later. Jean-Hugues