On 2013-05-23, at 14:28 , Benjamin Striegel wrote:

> There's no generalized notation, but a while ago I overloaded the +
> operator on Option such that it automatically unwraps them, adds the
> contained elements together (for any two types that implement Add), and
> returns a wrapped result.
> 
>    let foo = Some(1) + Some(4);
>    error!(foo);  // Some(5)
> 
> 
> This behavior seems mostly innocuous, but there's some question as to
> whether the behavior is correct (and also whether we want to support this
> sort of overloading at all): https://github.com/mozilla/rust/issues/6002
> 
> If this sort of thing is useful, it's feasible that we could overload the
> other operators as well.

I think that's way too specialized and repetitive for the general case.
A form of zip/map_zip for option would be better as it'd allow applying
arbitrary operations cleanly, something along the lines of fn zip<T,
U>(t: Option<T>, u: Option<U>) -> Option<(T, U)>

And thus you would write something along the lines of:

    let foo = option::zip(Some(1), Some(2)).map(|&(a, b)| { a + b })

zip would be something along the lines of:

    match (t, u) {
        (Some t1, Some v1) => Some (t1, v1),
        _ => None
    }

(well it would require pointer and lifetime powder thing, but you get the idea)
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to