Allen's lambda syntax proposal

Allen Wirfs-Brock Allen.Wirfs-Brock at microsoft.com
Mon Dec 1 21:33:19 PST 2008


Below

>-----Original Message-----
>From: Maciej Stachowiak [mailto:mjs at apple.com]
>Can you give an example? Since ECMAScript's built-in control
>structures already have special syntax, it's hard to make anything
>look *exactly* like them.
>
>Here's my best shot at an example. Let's pretend you have a
>withOpenFile function that opens a file, does some work using the
>resulting file handle (assuming there were such things in some host
>extension), and guarantees the file handle will be closed whether on
>normal exit or exception, but passing exceptions through. How would a
>site of use look?
>
>withOpenFile(fileName, { |handle|
>     doSomething(handle);
>     doSomethingElse(handle);
>});
>
>
>withOpenFile(fileName, \(handle) {
>     doSomething(handle);
>     doSomethingElse(handle);
>});
>
>Both look a little odd but I'd say the latter resembles built-in
>constructs like for..in a tiny bit more.

Just so everybody knows where I'm coming from with this, in Smalltalk this might look something like:

FileHandle forName: filename do: [:handle|
        self dosomething: handle.
        Self doSomethingElse: handle]

Which is syntactically very similar to the "built-in" control constructs such as:

1 to: max do: [:n| self doSomething: n].

The BGGA Java closure proposal attempts to support control abstractions that look like built in control constructs by allowing trailing literal closure arguments to appear after the parenthesized argument list (shades of Ruby).  If we did something similar in JavaScript you might then code your example as:

withOpenFile(fileName)  { |handle|
     doSomething(handle);
     doSomethingElse(handle);
};

which (to me) feels a little more like a built-in  control structure than

withOpenFile(fileName)  \{handle) {
     doSomething(handle);
     doSomethingElse(handle);
};

However, neither one is perfect. The Java closure proposal has some additional syntactic embellishments that attempt to make it even a bit closer but in my opinion they may have exceeded to point of diminishing returns.

>...
>
>I think it's possible to build useful control abstractions with a
>concise block-like syntax. But an important factor here is making
>these look natural in the context of the language as well as concise.
>Ruby and Smalltalk have overall designs that make them friendly to a
>smalltalkish block syntax, so your custom control constructs look just
>like the built-in ones (or conversely you could claim none are built
>in). In ECMAScript you can't have them look exactly the same, but I
>think the Haskellish backslash style fits in a little better.
>

I largely agree with this view.  Preserving the existing feel of a language is very important and it is hard to retrofit control abstraction facilities into a language that didn't have them in its initial design. Neither the backslash or vertical bar syntax is a perfect fit with the existing language and I could live with either although I personally like the vertical bar's homage to Smalltalk.

Allen



More information about the Es-discuss mailing list