[rust-dev] bikeshed on closure syntax

Patrick Walton pwalton at mozilla.com
Tue Apr 17 17:29:56 PDT 2012

On 4/17/12 4:57 PM, Graydon Hoare wrote:
> On 12-04-17 04:01 PM, Niko Matsakis wrote:
>> Apologies in advance.
> Ha! Apologies in return, then, as at least the first bit I'm kinda sour
> on. The rest I'm just confused over.
>> This requires arbitrary lookahead to disambiguate from tuples.
> This bit in particular. Really really don't want to cross the bridge to
> arbitrary lookahead in the grammar.

Yes, I don't like this either.

Note that these two are not problematic: "-> foo" (no lookahead) or "x 
-> x + 1" (one token lookahead). Only multi-arity functions require 
arbitrary lookahead: "(x, y) -> x + y". This could be fixed with some 
sort of sigil: "\(x, y) -> x + y" or bars: "|x, y| -> x + y". I kind of 
like the latter, although I'm told that JavaScript developers balked 
when TC39 proposed it.

>> 2. You may omit the last argument of a function that expects a closure
>> using a syntax like the following:
>>      vec.iter: x { ... }

This is ambiguous. Is "{ spawn: { hello() } }" a call to a function 
"spawn" with a block argument or a record literal with "spawn" as the key?

This can be repaired by requiring the parentheses:

     for [ 1, 2, 3 ].each(): x {

     spawn(): {

Or it can be repaired by requiring some sort of prefix for record literals.

> I guess I'm having a hard time seeing the motive. Is it a preference for
> parens over braces? We could probably support (|pat| expr) as a lambda
> just as well as {|pat| expr} -- parser can see the transition point to
> pattern grammar -- though it loses the "transitions to multi-line
> easily" aspect I mention in point #2 above..

To me (just IMHO) there are three main issues with the current syntax:

(1) In curly-brace-structured languages, "{" generally ends the line.

(2) "{||" reads like line noise.

(3) Programmers don't seem to like Ruby/Smalltalk-like bars much in 
languages that aren't in the syntactic tradition of Smalltalk. I didn't 
realize this when I proposed it... that said, I do like the way "|x, y| 
x + y" looks.

Anyway, just a little more shameless bikeshedding...


More information about the Rust-dev mailing list