Here's a total bikeshed. Apologies in advance:

There's been some criticism of Rust's syntax for being too brace-heavy. I've been thinking this for a while. Here's a minimal delta on the current syntax to address this:

Examples:

        // before:
        if foo() == "bar" { 10 } else { 20 }

        // after:
        if foo() == "bar" then 10 else 20
        // or:
        if foo() == "bar" { 10 } else { 20 }

        // before:
        alt foo() {
            "bar" { 10 }
            "baz" { 20 }
            "boo" { 30 }
        }

        // after:
        alt foo() {
            "bar" => 10,
            "baz" => 20,
            "boo" => 30
        }
        // or:
        alt foo() {
            "bar" { 10 }
            "baz" { 20 }
            "boo" { 30 }
        }

BNF:

        if ::== "if" expr ("then" expr | block) ("else" expr)?
        alt ::== "alt" expr "{" (arm* last-arm) "}"
        arm ::== block-arm | pat "=>" expr ","
        last-arm ::== block-arm | pat "=>" expr ","?
        block-arm ::== pat block

You can think of it this way: We insert a "then" before the then-expression of each if; however, you can omit it if you use a block. We also insert a "=>" before each expression in an alt arm and a "," to separate expressions from subsequent patterns; however, both can be omitted if the arm expression is a block.

This does, unfortunately, create the dangling else ambiguity. I'm not sure this is much of a problem in practice, but it might be an issue.

The pretty printer would always omit the "then" and the "=>"/"," when the alt arm is a block. That way, we aren't introducing multiple preferred syntactic forms of the same Rust code (which I agree is generally undesirable); the blessed style is to never over-annotate when a "then" body or an alt expression is a block.

Here's an example piece of code (Jonanin's emulator) written before-and-after:

Before: https://github.com/Jonanin/rust-dcpu16/blob/master/asm.rs
After: https://gist.github.com/2360838

Thoughts?

Patrick
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to