OCaml:如何使用电池和ppx_deriving。* togather?

时间:2017-02-28 16:22:14

标签: ocaml ppx

目前,我尝试将Batteriesppx_deriving.show或类似的东西一起使用 我想知道如何有效地使用它们。

要创建转储函数,我觉得ppx_deriving.show很有用。 但是我有点麻烦,像下面这样一起使用它们。

open Batteries
type t = { a: (int,int) Map.t }
[@@deriving show]

现在Map.pp未定义,因此无法编译。

我的特别修复是我创建module Map,其中包含Batteries.Map并定义了函数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]

它有效,但我很难适应所有的数据结构...... Core ppx_deriving.sexpBatteries的唯一选择,但我更喜欢ppx_deriving.showbokeh serve 。 有人知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

你的修复是正确的方法。如果您要对不使用M.t声明的数据类型[@@deriving]使用派生,则必须自己为M.pp提供show等方法:

module M = struct
  include M
  let pp = ... (* code for pretty-printing M.t *)
end

有一种方法可以部分自动化:

module M = struct
  include M
  type t = M.t = ... (* the same type definition of M.t *)
    [@@deriving show]
end

使用M.ppt生成deriving

使用ppx_import,您可以避免复制和粘贴定义:

module M = struct
  include M
  type t = [%import: M.t]
    [@@deriving show]
end

这应该扩展到以前的代码。

正如您所知,导出show的{​​{1}}并不是很有用:通常您不希望看到Map.t的二叉树表示,除非您正在调试{{ 1}}模块本身。

相关问题