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

Attachment: smime.p7s
Description: S/MIME cryptographic signature

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

Reply via email to