Add Result.orElse and Option.orElse
Dean177 opened this issue · comments
These should work similarly to the corresponding or_
functions but with the arguments swapped:
Ok "the value I want"
|> Result.orElse (Ok "a default value")
= Ok “the value I want”
Hey, i want to take a look at this.
Can you give me more context please
Sure thing, I updated my original example to show the result.
Here is a bit more detail:
Result.or_ a b chooses a if it's Ok and otherwise chooses b. Result.orElse a b chooses b if it's Ok and otherwise chooses a. Particularly useful in a pipe:
Error "Snakes!"
|> Result.orElse (Ok "a default value")
= Ok "a default value"
The motivation for this function is being able to use Result.or_
in a pipeline (|>
)
Feel free to shoot me a message (twitter @Dean177 / email deanmerchant@gmail / discord Dean#7331 ) if you aren't sure what you need to do as it will be a big help to streamline the process for future contributors
I had a question, does the Result module have an orElse function?
Because I couldn't find it in -> https://caml.inria.fr/pub/docs/manual-ocaml/libref/Result.html
Found it in bucklescript directory, but for Option type -> https://github.com/darklang/tablecloth/blob/master/bucklescript/src/TableclothOption.ml
Also there are 2 TableclothResult.ml one in native and one in bucklescript
Also there are 2 TableclothResult.ml one in native and one in bucklescript
Yeah, those are two different packages. One is tablecloth-native on opam, the other is tablecloth-bucklescript on npm
There seems already a Option.orElse
but not a Result.orElse
. Mind if I chip in?
Please do!