[rust-dev] Brace-free if and alt

Benjamin Striegel ben.striegel at gmail.com
Wed Apr 11 13:52:39 PDT 2012


Really not a fan of the alternative `if` syntax, and I think that the
problem of "too many braces" applies primarily to `alt` expressions anyway.
Another reason to be wary of your `if` proposal is that it makes Rust's
already-a-little-scary semicolon rules a bit harder to express ("you don't
need a semicolon after a closing brace in a control flow expression
statement that is not a component of a larger statement" is already about
at the threshold of credulity without adding "...or immediately after an
`if` statement making use of the alternative syntax").

An alternative `alt` syntax could be cool. One thing that I like about the
current syntax with the required braces is that it flows nicely from the
understanding "this is the end of a block, therefore this is where the
implicit `ret` exists", which is handy enough that it helps get over the
initial anxiety of semicolon significance. What if your alternative syntax
used a colon rather than a fat arrow, to mirror record literal syntax?

On Wed, Apr 11, 2012 at 4:28 PM, Patrick Walton <pwalton at mozilla.com> wrote:

> 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<https://github.com/Jonanin/rust-dcpu16/blob/master/asm.rs>
> After: https://gist.github.com/**2360838 <https://gist.github.com/2360838>
>
> Thoughts?
>
> Patrick
> ______________________________**_________________
> Rust-dev mailing list
> Rust-dev at mozilla.org
> https://mail.mozilla.org/**listinfo/rust-dev<https://mail.mozilla.org/listinfo/rust-dev>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20120411/1e0ff669/attachment.html>


More information about the Rust-dev mailing list