在 ml 文件中嵌入顶级指令
Embed top-level directive in ml files
我可能以错误的方式考虑了顶级。将顶级指令集合作为项目的一部分进行维护的首选方法是什么?
有没有办法在 OCaml 源文件中包含像 #install_printer
这样的顶级指令,以便在程序编译时忽略它们,但在 运行 时由顶级执行?
理想情况下,即使指令本身被忽略,我也希望在编译程序时对指令进行类型检查,例如
type 'a with_infinity = Finite of 'a | Infinite
let print_int_with_infinity pp item =
(match item with
| Infinite -> Format.pp_print_string pp "Infinite"
| Finite i -> Format.pp_print_int pp i)
(* install printer cannot occur in this context *)
#install_printer print_int_with_infinity
没有预定义的方法,但您可以让预处理器在编译时删除该指令。
为了对原语进行类型检查,你能做的最好的事情就是将它预处理成类似
的东西
#install_printer some_function
至
let _ = (some_function:Format.formatter -> 'a -> unit)
使用顶层的一个好方法是在项目的根目录下有一个 .ocamlinit
文件。当您从同一目录启动 utop
或 ocaml
时加载此文件。
通常看起来像这样:
#use "topfind";;
#require "this";;
#require "that";;
let _printer = ...;;
#install_printer _printer;;
...
相关说明,如果环境变量 OCAMLPATH
设置为 /path/to/my/project:...
,并且 /path/to/my/project/foo
中有合适的 META
文件,则可以使用 #require "foo"
.
加载 project-local foo
库及其依赖项
我可能以错误的方式考虑了顶级。将顶级指令集合作为项目的一部分进行维护的首选方法是什么?
有没有办法在 OCaml 源文件中包含像 #install_printer
这样的顶级指令,以便在程序编译时忽略它们,但在 运行 时由顶级执行?
理想情况下,即使指令本身被忽略,我也希望在编译程序时对指令进行类型检查,例如
type 'a with_infinity = Finite of 'a | Infinite
let print_int_with_infinity pp item =
(match item with
| Infinite -> Format.pp_print_string pp "Infinite"
| Finite i -> Format.pp_print_int pp i)
(* install printer cannot occur in this context *)
#install_printer print_int_with_infinity
没有预定义的方法,但您可以让预处理器在编译时删除该指令。
为了对原语进行类型检查,你能做的最好的事情就是将它预处理成类似
的东西#install_printer some_function
至
let _ = (some_function:Format.formatter -> 'a -> unit)
使用顶层的一个好方法是在项目的根目录下有一个 .ocamlinit
文件。当您从同一目录启动 utop
或 ocaml
时加载此文件。
通常看起来像这样:
#use "topfind";; #require "this";; #require "that";; let _printer = ...;; #install_printer _printer;; ...
相关说明,如果环境变量 OCAMLPATH
设置为 /path/to/my/project:...
,并且 /path/to/my/project/foo
中有合适的 META
文件,则可以使用 #require "foo"
.
foo
库及其依赖项