Currently I'm trying to use Batteries with ppx_deriving.show or something similar.
I am wondering how to use them together usefully.
To create dumping functions, I feel ppx_deriving.show is useful. But I have a little bit troubles using them together like the followings.
open Batteries
type t = { a: (int,int) Map.t }
[@@deriving show]
Now Map.pp is not defined, so it can't be compiled.
My adhoc fix is that I create module Map which include Batteries.Map and define function pp.
open Batteries
module Map = struct
include Map
let pp f g fmt t = ... (* create dump function by man hand *)
end
type t = { a: (int,int) Map.t }
[@@deriving show]
It works, but it is painful for me to adapt all of data structures...
Core with ppx_deriving.sexp is an alternative choice, but I prefer Batteries with ppx_deriving.show.
Does anybody know how to solve the problem?
Your fix is the right way. If you want to use deriving for data types M.t declared without [@@deriving], you have to give its methods such as M.pp for show by yourself:
module M = struct
include M
let pp = ... (* code for pretty-printing M.t *)
end
There is a way to partially automate this:
module M = struct
include M
type t = M.t = ... (* the same type definition of M.t *)
[@@deriving show]
end
It generates M.pp for type t using deriving.
With ppx_import, you can avoid copy-and-pasting of the definition:
module M = struct
include M
type t = [%import: M.t]
[@@deriving show]
end
This should be expanded to the former code.
As you have found out, deriving show of Map.t is not really useful though: normally you do not want to see the binary tree representation of Map.t unless you are debugging Map module itself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With