[rust-dev] Proposal: different alt form flavours

Stefan Plantikow stefan.plantikow at googlemail.com
Wed Dec 21 11:52:52 PST 2011

Am 21.12.2011 um 17:19 schrieb Marijn Haverbeke:

> This was prompted by the 'minor annoyances thread'. A big annoyance
> for me is the recurring `_ {}` at the end of alt patterns that fall
> through. A worse form is `_ { fail "this is a bug"; }` (alt already
> fails when not matching, with a line number reference to the alt that
> blew up, so this kind of `fail` statements mostly just muddle up the
> code).
> Instead, I propose:
>    // This one falls through without failing
>    alt myoption pass {
>       some(x) { do_something_with(x); }
>    }
>    // This one explicitly says that is is non-exhaustive, and that
>    // it will fail for bad input
>    alt myoption fail {
>       some(x) { ... }
>    }
> Normal alt would be required to be exhaustive, and give a compile-time
> error if it isn't. This is more self-documenting (you're explicitly
> annotating what you're trying to do when writing a non-exhaustive
> alt), and somewhat more succinct.
> I'm not terribly attached to those keywords (`pass` and `fail`), would
> happily hear alternatives.

I like the general idea of giving the default behaviour of alt (pass, fail, default value) a special status in the form of some "else" construct because
it will allow extending alt to try multiple values until one matches before
moving on to the else-branch (imagine `alt val1, val2, ... or even alt over vectors). However:

- alt now can be a statement or an expression, maybe as a statement, it should use a different keyword altogether "on myopt { ... }"
- it is quite unsusual control flow, normally one would expect either an else branch (i.e. alt { } else { }) or `fail`, and `pass` to be the last clauses int the alt block, i.e. `alt myopt { branch1; branch2; fail }`
- at first glance it is not clear what is meant by this (I would at least add an mandatory `else` keyword to help)
- I think this should be extended with a more terse way to specify a
default value that is statically guaranteed to match the left side

Beyond that it feels quite right and is nicely terse. 


More information about the Rust-dev mailing list