首页 > 解决方案 > Re.re 和 sexp.opaque :无法编译

问题描述

我无法成功编译以下代码

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

由于声明,这不应该发生(这将避免从见janestreet ppx_sexp_conv[@sexp.opaque]返回一个 sexp 表单)Re.re

我正在使用ocaml-4.07.1.

标签: ocaml

解决方案


看起来这个功能还没有向公众发布,可能它会作为发布的一部分v0.13发布。

如果我们查看最新(2019 年 4 月)ppx_sexp_conv 包的 README 文件,我们将找不到任何提及[@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]

推荐阅读