来自Real第17章的以下代码:使用S-表达式页339进行的数据序列化没有在utop上编译:
type http_server_config = { web_root: string;
port: int with default(80);
addr: string with default("localhost");
} [@@deriving sexp] ;;
它在抱怨with
in port: int with default(80);
谢谢!
发布于 2017-05-25 10:03:02
在最近版本的Janestreet核心库中,所有语法扩展都已从camlp4扩展转换为ppx扩展。因此,不幸的是,您需要在Real中使用语法扩展来调整所有示例的语法。
幸运的是,与camlp4扩展相反,ppx扩展不能广泛地修改OCaml语法。与普通的OCaml相比,它们最多可以使用稍微扩展的语法,wich添加扩展节点和属性。
特别是,这意味着由于field:type with …
对于普通的OCaml不是同步有效的,它也不是启用ppx_sexp_conv
扩展的有效语法。在您的示例中,默认值注释需要写入相应记录字段的属性:
type http_server_config = {
web_root: string;
port: int [@default 80];
addr: string [@default "localhost"];
} [@@deriving sexp] ;;
请注意,要在utop中工作,首先需要使用ppx_sexp_conv
扩展并打开默认运行时模块:
#require "ppx_sexp_conv";;
open Sexplib.Std ;;
type http_server_config = {
web_root: string;
port: int [@default 80];
addr: string [@default "localhost"];
} [@@deriving sexp] ;;
发布于 2017-05-25 07:56:19
Real中的示例假设您已经安装了Core,其中包括一些语法扩展。我按照安装说明这里和我仍然看到了你看到的问题。
当我试图安装语法扩展模块时,我看到了以下内容:
utop # #camlp4o;;
utop was built without camlp4 support.
因此,我的结论是,设置真实世界OCaml所期望的环境有点棘手。
无论如何,@Basile_Starynkevitch是正确的。代码使用的是在utop中不支持的语法扩展。
https://stackoverflow.com/questions/44182628
复制相似问题