Function Syntax

Brendan Eich brendan at mozilla.com
Thu May 12 14:25:50 PDT 2011


On May 12, 2011, at 1:06 PM, Brendan Eich wrote:

> On May 12, 2011, at 10:55 AM, Brendan Eich wrote:
> 
>> Ruby is far from simple, btw. Check out
>> 
>> http://samdanielson.com/2007/3/19/proc-new-vs-lambda-in-ruby
>> 
>> and the wikipedia page it references.
>> 
>> Looks like Proc.new but not lambda can return from its caller.
> 
> From http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Method_Calls it should be clear I was missing the "block" target. Blocks are syntactically restricted to being downward funargs. Only if reified as Procs do they potentially escape to be called later when their lexical parent method could have already returned.
> 
> IOW, blocks are restricted to being downward-funargs by syntax at their expression site, and by default in the callee (without the & before the corresponding formal parameter).

To say a bit more about this, here's a demo of the downward-only-funarg nature of blocks passed as extra trailing arguments, with no matching &parameters:

def say
    puts yield "world"
end

def say_hello
    say {|x| "hello #{x}" }
end

say_hello

The output is "hello world" of course, but Ruby's yield calls the block without it escaping as a reified Proc that could be invoked later, after the downward flow. Neat!

(Rubyists, please correct anything wrong here.)

I'm not suggesting we copy any of this, just passing along my Ruby-n00b knowledge. 


> When we considered lambdas (the "Allen's lambda syntax proposal" thread from late 2008 to early 2009), we did not try to confine them syntactically to actual parameter lists. Did we miss a key restriction or feature of Ruby? I'm not sure, I'm too much a Ruby n00b.

If blocks could not escape to be called after their enclosing function had returned, then we would overcome the objection raised last time, articulated best by Maciej:

https://mail.mozilla.org/pipermail/es-discuss/2008-December/008390.html

But Ruby went all the way, allowing a block to grow into a Proc and outlive the method in which the block was expressed. I expect similar "ecological pressures" to apply if we added blocks only as downward-funargs.

Plus, we'd still want shorter function syntax, not just blocks as downward-only funargs (however nice for map, forEach, etc.).

I will write up a block strawman, to give it a fair shake along side http://wiki.ecmascript.org/doku.php?id=strawman:arrow_function_syntax.

/be
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110512/41f1f155/attachment.html>


More information about the es-discuss mailing list