目前,我正试图将Batteries与ppx_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.sexp是另一种选择,但我更喜欢Batteries和ppx_deriving.show。有人知道如何解决这个问题吗?
发布于 2017-03-01 04:46:20
你的解决办法是正确的。如果您想对M.t声明的没有[@@deriving]的数据类型使用派生,您必须给出它的方法,比如M.pp for 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.pp为t类型生成deriving。
使用ppx_import,您可以避免复制和粘贴定义:
module M = struct
include M
type t = [%import: M.t]
[@@deriving show]
end这应该扩展到以前的代码。
正如您已经发现的,派生show of Map.t并不真正有用:通常您不希望看到Map.t的二叉树表示形式,除非您正在调试Map模块本身。
https://stackoverflow.com/questions/42513872
复制相似问题