Skip to content

RFC: Small closure syntax tweak #2394

Closed
@pcwalton

Description

@pcwalton

Might as well submit this as an RFC. I propose this syntactic tweak:

Expr ::== ... | Call | BlockLambda
BlockLambda ::== '|' AbbreviatedArgs '|' '{' (Stmt ';')* Expr '}'
Call ::== Primary '(' (Expr (',' Expr)*)? ')' (':' BlockLambda)?

In other words, keep the closure syntax exactly the same, but move the sticks before the initial curly brace, and add a colon before the sticks in the Ruby-like call notation.

The reason is to keep the brace at the end of the line:

for [ 1, 2, 3 ].each: |x| {
    io::println(#fmt(x))
}

As opposed to (currently):

for [ 1, 2, 3 ].each { |x|
    io::println(#fmt(x))
}

The former looks more familiar to C programmers (and Ruby/Smalltalk programmers) and looks nicer to me. The reason is that the brace is at the end of the line, allowing the eye to follow control the same way the user does in C.

The other nice things about this syntax are forward-thinking: it might allow us to drop the braces in the future, and it might allow us to use the Primary '{' ... '}' syntactic space for other things.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions