Haskell >>= or Rust ? for handling option<'t>?

For handling option<'t>, does ReScript offer anything like Rust’s ? or Haskell’s >>= ?

There is no support for custom infix operators anymore. So the answer is no.
You could overwrite a handful of operators like “+” and “-”, but I would say that’s a bad idea.

Well, there is a discussion about optional chaining: [RFC] Automatic optional chaining

1 Like

Asking just in case: I suppose you’re aware of Option.flatMap and asking about the infix operator for it specifically?

3 Likes

Was not aware of Option.flatMap. Thanks!

1 Like

With pipe-first style it’s pretty reasonable. You can even alias it to a short name and compress it down a bit, e.g.:

let opt = Belt.Option.flatMap

let optAdd = (optX, optY) =>
  optX->opt(x =>
  optY->opt(y =>
  x + y))

If you use the autoformatter it’s going to indent things around a bit, but it’s basically the same.

1 Like