Skip to content
This repository was archived by the owner on Apr 24, 2021. It is now read-only.

Commit 74b9a52

Browse files
committed
Make the unmonad script turn the let% into switch instead
Follow-up of #87 Now we can properly add error handling to these `None`/`Error(_)` later
1 parent 8f5c769 commit 74b9a52

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

src/ppx2/Ppx_Unmonads.re

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -115,21 +115,31 @@ let mapper =
115115
| Pexp_extension(({txt: (
116116
"opt" | "opt_consume"
117117
| "try" | "try_wrap"
118-
) as txt, loc}, PStr([{pstr_desc: Pstr_eval({pexp_desc: Pexp_let(Nonrecursive, bindings, continuation)}, _attributes)}]))) => {
119-
let (front, explanation) = switch (txt) {
120-
| "opt" => ([%expr Monads.Option.bind], opt_explanation)
121-
| "opt_consume" => ([%expr Monads.Option.consume], opt_consume_explanation)
122-
| "try" => ([%expr Monads.Result.bind], "Sugar for the Result type")
123-
| "try_wrap" => ([%expr Monads.Result.map], "Sugar for the Result type - auto-wraps in `Ok()`")
124-
| _ => assert(false)
125-
};
118+
) as txt}, PStr([{pstr_desc: Pstr_eval({pexp_desc: Pexp_let(Nonrecursive, bindings, continuation)}, _attributes)}]))) => {
126119
let (pat, expr) = process_bindings(bindings);
127-
Ast_helper.Exp.attr(
128-
[%expr [%e front]([%e mapper.expr(mapper, expr)], ~f=([%p pat]) => [%e mapper.expr(mapper, continuation)])],
129-
({txt: "ocaml.explanation", loc}, PStr([
130-
Ast_helper.Str.eval(Ast_helper.Exp.constant(Pconst_string(explanation, None)))
131-
]))
132-
)
120+
switch (txt) {
121+
| "opt" =>
122+
[%expr [%e [%expr switch [%e mapper.expr(mapper, expr)] {
123+
| None => None
124+
| Some([%p pat]) => [%e mapper.expr(mapper, continuation)]
125+
}]]]
126+
| "opt_consume" =>
127+
[%expr [%e [%expr switch [%e mapper.expr(mapper, expr)] {
128+
| None => ()
129+
| Some([%p pat]) => [%e mapper.expr(mapper, continuation)]
130+
}]]]
131+
| "try" =>
132+
[%expr [%e [%expr switch [%e mapper.expr(mapper, expr)] {
133+
| Error(e) => Error(e)
134+
| Ok([%p pat]) => [%e mapper.expr(mapper, continuation)]
135+
}]]]
136+
| "try_wrap" =>
137+
[%expr [%e [%expr switch [%e mapper.expr(mapper, expr)] {
138+
| Error(e) => Error(e)
139+
| Ok([%p pat]) => Ok([%e mapper.expr(mapper, continuation)])
140+
}]]]
141+
| _ => assert(false)
142+
};
133143
}
134144
| _ => Ast_mapper.default_mapper.expr(mapper, expr)
135145
}

0 commit comments

Comments
 (0)