Implementing rust's ? operator

That it is. I think (but am not sure) that to implement Rust’s “?” syntax requires rewriting code after type-checking, but I’m not sure. That would be another example of hard-coding the error monad in the language.

Note that if you are willing to name your intermediate results (which is often a good idea anyways) you already have your ? notation here:

let ( let* ) = Result.bind

Not really, x? (and its predecessor try!(x)) are just short for:

match x with {
Ok(x) => x,
Err(e) => return Err(e.into())
}

Which uses the trait Into for coercion. There is no magic beyond traits, which are core to Rust anyway.

2 Likes

I’ve been thinking about this, and it seems like there’s still a problem with sequences of expressions? (viz. 1 ; 2 ; 3) ? I should think this thru carefully: maybe with a combination of let* and some program-rewriting, I can get the same effect as in Rust. That would be nice.

1 Like

Bite the bullet ! :–) unit result sequencing is rare.

let* () = e1 in 
let* () = e2 in 
Ok ()