2017-01-22 58 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])。