block-lambda revival

Brendan Eich brendan at mozilla.com
Thu Jun 23 18:44:27 PDT 2011


On Jun 23, 2011, at 5:16 PM, Mark S. Miller wrote:

> Arrow functions:
> 
>     keys: function() {
>       return this.getOwnPropertyNames().filter(
>         (name) -> this.getOwnPropertyDescriptor(name).enumerable );
>     }

This is not giving arrow functions their due, though. Just use => and all is well.

Yes, you have to remember, but as you note, the "which this?" cuts both ways.


> Simple # functions
> 
>     keys: function() {
>       return this.getOwnPropertyNames().filter(
>         #(name) { this.getOwnPropertyDescriptor(name).enumerable });
>     }

Aside: we don't have a live proposal to use # this way, with completion value as implicit return value. Full 'return' or something like the empty-label hack I sketched (prefix : followed by tail position) would be needed. Also, # for frozen value counterparts (records and tuples, as well as functions) seems a better use of that character.


> block lambdas
> 
>     keys: function() {
>       return this.getOwnPropertyNames().filter { |name|
>         this.getOwnPropertyDescriptor(name).enumerable
>       }
>     }
> 
> The first and obvious point I'm making is that Tom would not have made this particular mistake with block lambdas, but I recognize that this argument cuts both ways. Other uses may have the opposite hazard, and we need to examine this as well. The new point is that arrow functions and simple # functions make the original hazard *worse*, because, like "function", they still rebind this.

While -> does bind |this| dynamically, => does not.


> But by being less verbose than "function", it's even easier for the eye to miss them when seeing a "this" in their body and trying to determine what it means.

You're right that it's easy to miss the bad thing, even if there's a good thing.


> The more I think about this, the more I'm inclined to believe that, for functions which rebind "this", the verbosity of "function" is a virtue.

But it didn't help in the story you led with :-).


> As fallible programmers, we can only afford a more compact function notation for defining functions with lexical "this" -- like block lambdas.

Or =>?

I'm leaning toward block lambdas myself, but I feel the need to play umpire here a bit -- arrows need fair play including =>, not just ->.

/be


More information about the es-discuss mailing list