You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 23:45:01