Kapil
Kapil

Reputation: 423

Using sexplib for serializing Ocaml types

I'm trying to use sexplib to serialize and deserialize a custom ocaml record type which has an inline definition as follows

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]

When I try to compile this using ocamlfind ocamlc -package sexplib,ppx_sexp_conv -linkpkg microblog_app.ml I get the error Failure("Pcstr_record not supported") File "microblog_app.ml", line 1: Error: Error while running external preprocessor.

I saw that ppx_sexp_conv doesn't support inline definitions in their current release. Unfortunately I can't use their development release since it would cause a version conflict with my other packages. So I tried to change the inline definition as follows

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]

I only use the functions sexp_of_eff and eff_of_sexp in my later code. When I compile this I get the error Error: Unbound value usernamePwd_of_sexp. I don't at all use this function in my code. Can someone please tell me how to resolve this error?

Upvotes: 2

Views: 239

Answers (1)

Étienne Millon
Étienne Millon

Reputation: 3028

The [@@deriving sexp] annotation you added generates some code that calls usernamePwd_of_sexp (due to the way ppx works, it cannot know whether this function exists or not, so it relies on its existence).

This function does not exist. You can create it by adding [@@deriving sexp] to the usernamePwd declaration (and other types).

An alternative is to make your type declaration recursive (type usernamePwd = ... and leaderId = ... and type eff = ... [@@deriving sexp]).

Upvotes: 2

Related Questions