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. But obviously it's not quite the same as having a
generalized solution (and I do think that Options needs to be as nice to
use as possible in order for anyone to bother adopting them).
On Thu, May 23, 2013 at 2:13 AM, Ziad Hatahet <[email protected]> wrote:
> Say we want to implement the following function:
>
> fn add(x: Option<int>, y: Option<int>) -> Option<int> { ... }
>
> Some functional languages, like Haskell and Scala offer some sort of a
> "do" notation to make unwrapping multiple Option type values easier.
>
> add :: Maybe Int -> Maybe Int -> Maybe Int
> add mx my = do
> x <- mx
> y <- my
> return (x + y)
>
> Is there an equivalent construct in Rust?
>
> --
> Ziad
>
> _______________________________________________
> 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