block lambda revival, now with semantics

Brendan Eich brendan at mozilla.com
Mon May 23 08:25:22 PDT 2011


On May 23, 2011, at 7:06 AM, Jorge wrote:

> On 23/05/2011, at 07:15, Kam Kasravi wrote:
> 
>> Is this valid?
>> 
>> function Person(a) {
>>  this.age = a;
>> }
>> Person.prototype.myage = {|| this.age};
>> 
>> function info(myage) {
>>  console.log('my age is '+myage());   
>> }
>> info(new Person(10).myage);
>> info(new Person(12).myage);
> 
> 
> If it's valid (I don't know if there can be a block-lambda standing alone outside of a function), it would return the value of the global variable `age`, I think, because the enclosing scope's `this` in that code above seems to be the global Object.

Yes, see my reply. The code in between { and } (including {|...| and }, even when used as an expression) can be factored out without names changing meaning.


> Brendan, do you really want these block-lambdas to be blocks?

Of course, that's part of the proposal and we've discussed this since at least late 2008. See the link from the Prologue at

http://wiki.ecmascript.org/doku.php?id=strawman:block_lambda_revival


> I thought you were just going to borrow its syntax for shorter function(){} semantics, not blocks semantics ?

Please read the strawman.


> What if {||} were just the shorter function syntax we want, with the added features we want (completion value as return value, lexical/dynamic `this`, ... ?), but not true blocks ?

No, it does not look like a function. It looks like a Ruby block, based on Smalltalk blocks which had different syntax. Different semantics for different syntax.

/be


More information about the es-discuss mailing list