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

Vassily Gavrilyak gavrilyak at gmail.com
Sun Mar 18 08:33:39 PDT 2007


On 3/18/07, Yuh-Ruey Chen <maian330 at gmail.com> wrote:
> Vassily Gavrilyak wrote:
> > I already proposed it.
> >
> > >>File.open("file", (file))=>{
> > >>   //process
> > >>});
> > >>May be C# way will be good, and C# is ECMA too. It has the same
> > superfluous () as ES3, >>but looks good.
> >
> > More examples from C# spec
> > x => x + 1                                                      //
> > Implicitly typed, expression body
> > x => { return x + 1; }                  // Implicitly typed, statement body
> > (int x) => x + 1                                        // Explicitly
> > typed, expression body
> > (int x) => { return x + 1; }    // Explicitly typed, statement body
> > (x, y) => x * y                                 // Multiple parameters
> > () => Console.WriteLine()               // No parameters
> >
>
> I like it. As with any other syntax addition, I'd like the syntax to be
> as universal as possible so as to make it intuitive. Since it's
> equivalent to any other lambda syntax, it can be used within object
> literals as well. Using all 3 lambda syntaxes:
>
> obj = {
>     x: 0,
>     add: y => this.x + y,
>     increment: y => void (this.x += y)
> };
>
> obj = {
>     x: 0,
>     add: function(y) { return this.x + y; },
>     increment: function(y) { this.x += y; }
> };
>
> obj = {
>     x: 0,
>     add: function(y) this.x + y,
>     increment: function(y) void (this.x += y)
> };
>
> For non-void functions, the a => b syntax looks really nice. Since it is
> equivalent to function(a) b, I expect it to the have the same operator
> precedence (higher than comma I think).
>
> However, it can't be used for class methods, which I think is fine. But
> if you really want the syntax to be as universal as possible, I can
> imagine something like this:
>
> class Klass {
>     foo(x) => print(x)
> }
>
> or if parenthesis aren't required:
>
> class Klass {
>     foo x => print(x)
> }
>
> But that looks a bit to alien to me, since it's asymmetric with the call
> syntax: we do o.foo(x), not o.foo x.
>
> -Yuh-Ruey Chen
>
Never thought about using shortcut syntax for named functions,
but it's good  here too.
Parenthesis  can be made mandatory, not a big deal, however we tried to
avoid some parenthesis earlier in this list.
They are optional only with one argument in C#, but that's the most common case.
This syntax seed to work nice with getters and setters too.
The only place where it doesn't work is type declaration.
So something like this
function map(mapper: (function (_:*, _:uint, _:Object):*),
thisObj:Object):Array
will become this
function map(mapper: (_:*, _:uint, _:Object) => *, thisObj:Object):Array

Regards,
Vassily



More information about the Es4-discuss mailing list