我无法成功编译以下代码
open Base
open Sexplib.Std
module Myregexp = struct
type t =
| Default
| Regexp of
{ re : (Re.re [@sexp.opaque])
; a : int
}
[@@deriving sexp]
let default = Default
end
关联的dune
文件:
(library (name myregexp)
(libraries base re sexplib) (preprocess (pps ppx_jane ppx_sexp_conv)))
构建命令为:dune build myregexp.a
。
我收到错误消息:
File "myregexp.ml", line 9, characters 16-21:
Error: Unbound value Re.re_of_sexp
由于[@sexp.opaque]
语句(应该避免从Re.re
返回sexp表单,请参见janestreet ppx_sexp_conv),该情况不应发生
我正在使用ocaml-4.07.1
。
答案 0 :(得分:1)
该功能似乎尚未向公众发布,它可能会作为v0.13
版本的一部分发布。
如果我们查看最新的README文件(2019年4月)ppx_sexp_conv程序包,将找不到[@sexp.opaque]
$ opam source ppx_sexp_conv.v0.12.0
$ grep sexp.opaque ppx_sexp_conv.v0.12.0/README.org
converters, simply apply the qualifier =sexp_opaque= as if it were a
type foo = int * stuff sexp_opaque [@@deriving sexp]
我们看到,只有旧的sexp_opaque
技巧。因此,在当前时间点上剩下的就是使用它,例如,
type t =
| Default
| Regexp of
{ re : Re.re sexp_opaque;
; a : int
}
'a sexp_opaque
类型的构造函数被定义为'a sexp_opaque = 'a
,除了sexp转换器将其视为不透明元素。
最有可能的是,这将与JS库的将来发行版一起中断,因此,我建议您使用一个更加冗长但稳定的解决方案:
type regex = Re.t
let sexp_of_regex = sexp_of_opaque
let regex_of_sexp = opaque_of_sexp
type t =
| Default
| Regexp of
{ re : regex;
; a : int
}
[@@deriving sexp]