如何在utop环境中使用ppx_derive预处理器?
在utop中使用ppx_deriving的方法
首先确保你已经通过opam安装了所有必要依赖:
opam install ppx_deriving ppx_hash ppx_sexp_conv ppx_compare core_kernel
单次临时会话使用
- 打开utop后,依次执行以下命令加载依赖和预处理器:
#require "ppx_deriving";; #require "ppx_hash";; #require "ppx_sexp_conv";; #require "ppx_compare";; #require "core_kernel";;
- 加载完成后即可直接运行你给出的示例代码:
module A = struct module T = struct type t = int [@@deriving hash, sexp, compare] end include Core_kernel.Hashable.Make(T) end;;
永久配置utop默认加载
不需要每次启动utop都手动执行#require命令,可通过修改启动配置实现:
- 编辑用户目录下的
.ocamlinit文件(如果文件不存在可直接新建) - 将上述5条
#require命令添加到文件末尾后保存 - 重启utop后即可直接使用ppx_deriving相关功能和core_kernel库
内容的提问来源于stack exchange,提问作者David 天宇 Wong
相关产品推荐
相关产品推荐

