simple shorter function syntax
Peter van der Zee
ecma at qfox.nl
Mon Jul 26 02:15:43 PDT 2010
> On 26.07.2010 12:06, Cameron McCormack wrote:
>>
>> Sam Ruby:
>>
>>> [0,1,2,3].map {|x| x*x}
>>>
>>> (try it in 'irb' to see what I mean)
>>>
>>> While I don't believe that would fly here, perhaps adding
>>> parens around the function would:
>>>
>>> [0,1,2,3].map({|x| x*x})
>>>
>> I think having the braces on the outside would help with the
>> aforementioned problem. The vertical bars don't particularly
>> grab me (not being a Rubyist), but this does:
>>
>> [0, 1, 2, 3].map({ x => x * x }); // optionally { (x) => x * x
>> }?
>>
>> function mapPairs(o, f) {
>> for (var p in o) {
>> f(p, o[p]);
>> }
>> }
>>
>> mapPairs(someObject, { (a, b) => a + '=' + b) });
I don't really like the proposed alternatives for function so far. The single
character alternatives to "function", except perhaps for the lambda or florin
sign (being Dutch ;)), don't have the visual appeal. Just like Dmitry pointed
out.
I'm not sure all of this is really worth having a shorter function keyword. In
fact, it seems to contradict the ASI discussion where we're trying to prevent
problems. Readability seems to be an important concern... So introducing obscure
syntax, even at the cost of upping parser complexity, seems weird to me.
Having said that. I like the ruby syntax proposed by Sam. If you combine the
opening curly with an existing operator and require it to be a literal (so no
space allowed between { and |, much like any other literal) you could have a
production like
func :: {| arguments | functionbody }
Which should be no problem for parsers. Obviously you can pick alternatives for
| and { but as long as you pick a combination that's currently illegal, there
should not be a problem.
My vote would go to leave function as it is, if not then to use lambda or florin
and otherwise something like the above. Reserve # and @ for new syntax.
+ peter
More information about the es-discuss
mailing list