I quite like this proposal, though I'd suggest that the "else" clause is evaluated as the value for when the pattern fails. `!` would always be substitutable there.
On Sun, Dec 22, 2013 at 10:47 AM, Gábor Lehel <[email protected]> wrote: > Using `match` works well enough, but if there's demand for a refutable `let` > which is lighter-weight, what about: > > let Some(result) = from_utf8(some_bytes) else fail!(); > > In other words, if the `let` pattern is refutable, you have to provide > something `else` with return type `!` as the alternative for when the `let` > fails to match. > > > (I could imagine that being generalized to any number of `else`s of which > only the last "returns `!`" (i.o.w. never returns), for example > > let Some(result) = from_utf8(some_bytes) else > from_utf8(some_other_bytes) else fail!(); > > and/or to allowing anything `else` which always matches, e.g. > > let Some(result) = from_utf8(some_bytes) else Some("default"); > > of which anything that "returns" `!` would be only a special case. But these > have progressively diminishing returns, and I'm merely mentioning, not > endorsing them.) > > > On Tue, Dec 17, 2013 at 9:11 PM, Kevin Ballard <[email protected]> wrote: >> >> On Dec 17, 2013, at 11:37 AM, Stefan Plantikow >> <[email protected]> wrote: >> >> Hi, >> >> Am 17.12.2013 um 20:10 schrieb Corey Richardson <[email protected]>: >> >> On Tue, Dec 17, 2013 at 2:06 PM, Stefan Plantikow >> <[email protected]> wrote: >> >> Hi, >> >> >> Am 09.12.2013 um 16:53 schrieb Damien Radtke <[email protected]>: >> >> I have no idea if it would be feasible in the standard library, but >> wouldn't the ideal solution be having one function (e.g. from_utf8()) that >> could return two possible values, a bare result and an Option? Letting the >> compiler decide which version to use based on type inference like this: >> >> let result: ~str = from_utf8(...); >> let result: Option<~str> = from_utf8(...); >> >> Assuming both of them are passed invalid UTF8, then the first version >> would fail, but the second version would just return None. >> >> >> >> >> We already have pattern matching in `let` (the LHS is a pattern), but >> it's only for irrefutable patterns. IOW, `let` can never fail, and >> that's a very very useful property IMO. >> >> >> oh ok I haven’t kept up on the syntax then. Given the utility of >> destructuring bind for error handling, wouldn't it make sense to have a >> variant of let that can fail? >> >> Now syntax is a matter of practicality and taste but spontaneously this >> comes to mind: >> >> let opt Some(~result) = from_utf8(..) >> >> comes to mind. >> >> >> You can do it with a bit more verbosity, which I think is perfectly fine >> as it makes failure much more obvious. >> >> let result = match from_utf8(..) { >> Some(~result) => result, >> _ => fail!("b0rk b0rk b0rk") >> }; >> >> Of course, in this particular example, you'd probably just write >> >> let result = from_utf8(..).unwrap(); >> >> but the longer match form will work for other enums. >> >> -Kevin >> >> _______________________________________________ >> Rust-dev mailing list >> [email protected] >> https://mail.mozilla.org/listinfo/rust-dev >> > > > _______________________________________________ > Rust-dev mailing list > [email protected] > https://mail.mozilla.org/listinfo/rust-dev > _______________________________________________ Rust-dev mailing list [email protected] https://mail.mozilla.org/listinfo/rust-dev
