On 07/12/13 20:55, Simon Sapin wrote:
On 07/12/2013 01:14, Huon Wilson wrote:
I personally think a better solution is something like Haskell's do
notation[1], where you can chain several computations that return
Option<..> such that if any intermediate one returns None, the later
ones are not evaluated and the whole expression returns None, which
saves having to call .get()/.unwrap()/.expect() a lot.

We have that, it’s Option’s .and_then() method.


This can work for types like Result too (in fact, the Haskell
implementation of `do` is sugar around some monad functions, so any
monad can be used there; we currently don't have the power to express
the monad typeclass/trait in Rust so the fully general form probably
isn't possible as a syntax extension yet, although a limited version is).



Yes, that's what my syntax extension[1] (to which Ziad alludes) desugared to (well, the former name, .chain).

However, even something simple like `foo.and_then(|x| bar.and_then(|y| baz.map(|z| x + y + z)))` is significantly worse than

    do
        x <- foo
        y <- bar
        z <- baz
        return $ x + y + z

as it is in Haskell. And more complicated examples with additional within the closures are significantly worse for the .and_then form (leading to pyramids of doom, and Rust already has enough of those).


Huon

[1]: https://mail.mozilla.org/pipermail/rust-dev/2013-May/004182.html
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to