Expression closures - use-cases for shortcut lambda syntax(blocks)

Jeff Dyer jodyer at adobe.com
Mon Mar 19 13:59:11 PDT 2007


> > This is a trick question! We already have function expressions with
a
> > 'function' head and a '{...}' body. Dropping the 'return' and braces
as
> > a *convenience*.
> 
> Not a trick question -- of course we already have functions that *are*
> expressions; the question is whether all the syntactic forms of
> functions (i.e., both function declarations and function expressions)
> have a variant where the *body* is an expression.

Okay a tricky question then :)

> It's precisely this convenience I'm talking about. In fact, when
writing
> in a functional style, this convenience adds up pretty quickly. Try
> writing a curried function with "return" and curlies -- it's painful,
> *especially* because of the sneaky requirement that return can't be
> followed by a newline!
> 
>      function f(x) {
>          return function (y) {
>              return function (z) {
>                  return x + y + z;
>              }
>          }
>      }
> 
> vs.
> 
>      f(x) => (y) => (z) => (x + y + z)
> 
> or
> 
>      function f(x) (y) => (z) => (x + y + z)
> 
> I didn't quite follow your summary. Indulge my OCD and let me be
> painfully explicit. :) Here are all the options I think we're talking
> about, individually labelled (a) through (f). (Ignoring some
irrelevant
> details like named function expressions.)
> 
>     function-expression
>         ::= "function" id? "(" formals ")" block           (a)
>          |  "function" id? "(" formals ")" expression      (b)
>          |  "function" id? "(" formals ")" "=>" expression (c)
>          |  "(" formals ")" "=>" expression                (d)
> 
>     function-declaration
>         ::= "function" id "(" formals ")" block            (e)
>          |  "function" id "(" formals" ")" expression      (f)
>          |  "function" id "(" formals" ")" "=>" expression (g)
>          |  id "(" formals ")" "=>" expression             (h)
> 
>     expression ::= ... | function-expression
>     declaration ::= ... | function-declaration
> 
> I'm in favor of allowing (a), (c), (d), (e), and (g). I think that was
> Tucker's proposal just now.

I'm in favor of (a), (b), (d), (e), (f). As I've already said, (c) and
(g) are not as nice as (b) and (f) because the latter erase characters
from the current function expression form, while the former adds "=>"

To be consistent with the current expression form

    function id (x) { return x }

should be shortened to 

    function id(x) x

not

    function id(x) => x





More information about the Es4-discuss mailing list