ppx_deriving.showを使ってみる
ppxはpreprocessor的なもの。
opam install ppx_deriving
field.ml
type point2d = float * float [@@deriving show] type person = { name: string; age: int; } [@@deriving show] type student = { name: string; (* 最近はレコードに同名のkeyを付けてもokになったっぽい *) age: int; grade: int; } [@@deriving show] (* val pp_student : Format.formatter -> student -> unit val show_student : student -> string が生成される *) let () = let s = {name = "foo"; age = 10; grade = 3;} in print_string (show_student s)
ocamlbuildでbuildして実行してみる
$ ocamlbuild -use-ocamlfind -tag debug -tag bin_annot -pkg ppx_deriving.show field.native $ ./field.native { Field.name = "foo"; age = 10; grade = 3 }
他にも色々dervingが用意されている。ppx_deriving