-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
show: handle [@printer] in polymorphic variants #267
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -173,20 +173,31 @@ let rec expr_of_typ quoter typ = | |
| { ptyp_desc = Ptyp_variant (fields, _, _); ptyp_loc } -> | ||
let cases = | ||
fields |> List.map (fun field -> | ||
match field.prf_desc with | ||
| Rtag(label, true (*empty*), []) -> | ||
match attr_printer field.prf_attributes, field.prf_desc with | ||
| None, Rtag(label, true (*empty*), []) -> | ||
let label = label.txt in | ||
Exp.case (Pat.variant label None) | ||
[%expr Ppx_deriving_runtime.Format.pp_print_string fmt [%e str ("`" ^ label)]] | ||
| Rtag(label, false, [typ]) -> | ||
| Some printer, Rtag(label, true (*empty*), []) -> | ||
let label = label.txt in | ||
Exp.case (Pat.variant label None) | ||
[%expr [%e printer] fmt ()] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am worried about the use of |
||
| None, Rtag(label, false, [typ]) -> | ||
let label = label.txt in | ||
Exp.case (Pat.variant label (Some [%pat? x])) | ||
[%expr Ppx_deriving_runtime.Format.fprintf fmt [%e str ("`" ^ label ^ " (@[<hov>")]; | ||
[%e expr_of_typ typ] x; | ||
Ppx_deriving_runtime.Format.fprintf fmt "@])"] | ||
| Rinherit({ ptyp_desc = Ptyp_constr (tname, _) } as typ) -> | ||
| Some printer, Rtag(label, false, [typ]) -> | ||
let label = label.txt in | ||
Exp.case (Pat.variant label (Some [%pat? x])) | ||
[%expr [%e printer] fmt x] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same remarks: you could share more code (but not doing so is okay), and |
||
| None, Rinherit({ ptyp_desc = Ptyp_constr (tname, _) } as typ) -> | ||
Exp.case [%pat? [%p Pat.type_ tname] as x] | ||
[%expr [%e expr_of_typ typ] x] | ||
| Some printer, Rinherit({ ptyp_desc = Ptyp_constr (tname, _) }) -> | ||
Exp.case [%pat? [%p Pat.type_ tname] as x] | ||
[%expr [%e printer] fmt x] | ||
| _ -> | ||
raise_errorf ~loc:ptyp_loc "%s cannot be derived for %s" | ||
deriver (Ppx_deriving.string_of_core_type typ)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be easier to factorize the two cases better, the code is identical except for the second argument of
Exp.case
that depends on the optional printer attribute.