2017-01-22 61 views
2

我试图用sexplib序列化和反序列化如下使用sexplib序列化ocaml的类型

type eff = Add of {username: string; pwd: string} 
| AddFollowing of {leader_id: id} 
| RemFollowing of {leader_id: id} 
| AddFollower of {follower_id: id} 
| RemFollower of {follower_id: id} 
| Blocks of {follower_id: id} 
| GetBlocks 
| IsBlockedBy of {leader_id: id} 
| GetIsBlockedBy 
| GetInfo 
| GetFollowers 
| GetFollowing [@@deriving sexp] 

当我尝试编译这个使用ocamlfind ocamlc -package sexplib,ppx_sexp_conv -linkpkg microblog_app.ml我得到它有一个内联定义自定义ocaml的记录类型错误Failure("Pcstr_record not supported") File "microblog_app.ml", line 1: Error: Error while running external preprocessor

我看到ppx_sexp_conv不支持其当前版本中的内联定义。不幸的是,我不能使用他们的开发版本,因为它会导致与我其他软件包的版本冲突 。所以,我试图改变在线定义如下

type usernamePwd = {username: string; pwd: string} 
type leaderId = {leader_id: id} 
type followerId = {follower_id: id} 
type eff = Add of usernamePwd 
| AddFollowing of leaderId 
| RemFollowing of leaderId 
| AddFollower of followerId 
| RemFollower of followerId 
| Blocks of followerId 
| GetBlocks 
| IsBlockedBy of leaderId 
| GetIsBlockedBy 
| GetInfo 
| GetFollowers 
| GetFollowing [@@deriving sexp] 

我只用功能sexp_of_effeff_of_sexp在我后面的代码。当我编译这个时,我得到错误Error: Unbound value usernamePwd_of_sexp。我在代码中完全没有使用这个函数。有人可以告诉我如何解决这个错误?

回答

2

您添加的[@@deriving sexp]注释会生成一些调用usernamePwd_of_sexp的代码(由于ppx的工作方式,无法知道此函数是否存在,因此它依赖于它的存在)。

此功能不存在。您可以通过将[@@deriving sexp]添加到usernamePwd声明(和其他类型)来创建它。

另一种方法是让您的类型声明递归(type usernamePwd = ... and leaderId = ... and type eff = ... [@@deriving sexp])。