Skip to content

Commit

Permalink
Add support for recursive types
Browse files Browse the repository at this point in the history
Signed-off-by: Nathan Rebours <[email protected]>
  • Loading branch information
NathanReb committed Dec 14, 2023
1 parent b948835 commit abbce85
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
4 changes: 1 addition & 3 deletions deriver/ppx_deriving_dyn.ml
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,9 @@ module Impl = struct

let generate ~ctxt (rec_flag, type_declarations) =
let loc = Expansion_context.Deriver.derived_item_loc ctxt in
let rec_flag = really_recursive rec_flag type_declarations in
match type_declarations with
| [] -> assert false
| [ type_declaration ] ->
let value_binding = value_binding ~loc type_declaration in
[ Ast_builder.Default.pstr_value ~loc Nonrecursive [ value_binding ] ]
| type_decls ->
let value_bindings = List.map (value_binding ~loc) type_decls in
[ Ast_builder.Default.pstr_value ~loc rec_flag value_bindings ]
Expand Down
11 changes: 11 additions & 0 deletions test/deriver/test_to_dyn.expected.ml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ include
| `C (x0, x1) -> Dyn.variant "C" [Dyn.int x0; Dyn.string x1])
polymorphic_variant
end[@@ocaml.doc "@inline"][@@merlin.hide ]
type recursive =
| Leaf
| Node of recursive * recursive [@@deriving dyn][@@ocaml.warning "-37"]
include
struct
let rec recursive_to_dyn =
function
| Leaf -> Dyn.variant "Leaf" []
| Node (x0, x1) ->
Dyn.variant "Node" [recursive_to_dyn x0; recursive_to_dyn x1]
end[@@ocaml.doc "@inline"][@@merlin.hide ]
type mrec_1 =
| A of int
| B of mrec_2
Expand Down
5 changes: 5 additions & 0 deletions test/deriver/test_to_dyn.ml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ type polymorphic_variant =
]
[@@deriving dyn]

type recursive =
| Leaf
| Node of recursive * recursive
[@@deriving dyn] [@@ocaml.warning "-37"]

type mrec_1 =
| A of int
| B of mrec_2
Expand Down

0 comments on commit abbce85

Please sign in to comment.