this is a fact of life in rescript. you gotta be misinterpreting me, or i’m misinterpreting you. it’s the same with a switch statement today, each branch has to generate the same type output.
I’m looking here: rescript-core/src/Core__Option.res at main · rescript-lang/rescript-core · GitHub
there is no “map” function for None states, meaning you cant do something like value->Option.getOrElse(slowSyncOp). the value provided to getOr has to be computed before putting it into getOr. see what I mean?
same with andThen for result. these are not necessary, but provide some nice ergonomics.
i’m pulling code from a commercial project here so it’s a bit adjusted and lacks context (and also i’m pushing the boundaries of what i’m allowed to share):
let result = switch str->split {
| Ok(parts) => {
parts->Array.reverse
let rec processArgs = (acc) => {
switch parts->Array.pop {
| Some(name) =>
switch args->Dict.get(name)->Option.map(handleArg(_, parts)) {
| Some(Ok(value)) =>
switch acc->Dict.get(name) {
| Some(_) => Error(TooManyOccurrences(`Argument '${name}' must only be specified once`))
| None => {
acc->Dict.set(name, value)
processArgs(acc)
}
}
| Some(Error(e)) => Error(e)
| None => Error(UnrecognizedArg(`Unrecognized argument: '${name}'`))
}
| None => Ok(acc)
}
}
processArgs(Dict.make())
}
this could be dedented trivially with early returns