OCaml:自动化自定义漂亮打印机安装
OCaml: Automate custom pretty-printer installing
我已经为一个模块实现了一个漂亮的打印机。目前我启动 utop
,加载依赖项然后执行 #install_printer pp_custom;;
,其中 pp_custom
是漂亮的打印机。
我想自动执行此操作,以便我可以使用类似于 lacaml
库的方式来使用它,其中矩阵的漂亮打印机默认为 "installed"。
我该怎么做?
简而言之,每当您在顶部加载库时,都需要 运行 #install_printer
指令。我正在使用以下代码来评估顶层代码:
open Core_kernel.Std
open Or_error
let eval_exn str =
let lexbuf = Lexing.from_string str in
let phrase = !Toploop.parse_toplevel_phrase lexbuf in
Toploop.execute_phrase false Format.err_formatter phrase
let eval str = try_with (fun () -> eval_exn str)
它依赖于 Core_kernel
,但你可以很容易地摆脱它,只需使用 eval_exn
而不是 eval
(最后一个将可能的异常包装到 Or_error
单子)。一旦你获得了 eval
函数,它就可以用来加载你的打印机:
let () = eval (sprintf "#install_printer %s;;" printer)
其中 printer
是漂亮打印函数的名称(通常用模块名称限定)。通常,将此类代码放入单独的库中,命名为 library.top
,其中 library
是您的库的名称。
为了进一步自动化,您可以要求所有类型(您希望在顶层自动打印)在中央注册表中注册自己,然后调用所有已注册的打印机,而不是手动枚举它们。要立即查看所有这些工作,您可以查看 BAP library. It has a sublibrary called bap.top
that automatically installs all printers. Each type that is desired to be printable implements Printable
signature, using Printable.Make
functor, that not only derives many printing functions from the basic definition, but also registers the generated pretty-printers in Core_kernel's Pretty_printer
registry (you can use your own registry, this is just a set of strings, nothing more). When bap.top
library is loaded into the toplevel (using require
or load
directive), it enumerates all registered printers and install 它们。
我已经为一个模块实现了一个漂亮的打印机。目前我启动 utop
,加载依赖项然后执行 #install_printer pp_custom;;
,其中 pp_custom
是漂亮的打印机。
我想自动执行此操作,以便我可以使用类似于 lacaml
库的方式来使用它,其中矩阵的漂亮打印机默认为 "installed"。
我该怎么做?
简而言之,每当您在顶部加载库时,都需要 运行 #install_printer
指令。我正在使用以下代码来评估顶层代码:
open Core_kernel.Std
open Or_error
let eval_exn str =
let lexbuf = Lexing.from_string str in
let phrase = !Toploop.parse_toplevel_phrase lexbuf in
Toploop.execute_phrase false Format.err_formatter phrase
let eval str = try_with (fun () -> eval_exn str)
它依赖于 Core_kernel
,但你可以很容易地摆脱它,只需使用 eval_exn
而不是 eval
(最后一个将可能的异常包装到 Or_error
单子)。一旦你获得了 eval
函数,它就可以用来加载你的打印机:
let () = eval (sprintf "#install_printer %s;;" printer)
其中 printer
是漂亮打印函数的名称(通常用模块名称限定)。通常,将此类代码放入单独的库中,命名为 library.top
,其中 library
是您的库的名称。
为了进一步自动化,您可以要求所有类型(您希望在顶层自动打印)在中央注册表中注册自己,然后调用所有已注册的打印机,而不是手动枚举它们。要立即查看所有这些工作,您可以查看 BAP library. It has a sublibrary called bap.top
that automatically installs all printers. Each type that is desired to be printable implements Printable
signature, using Printable.Make
functor, that not only derives many printing functions from the basic definition, but also registers the generated pretty-printers in Core_kernel's Pretty_printer
registry (you can use your own registry, this is just a set of strings, nothing more). When bap.top
library is loaded into the toplevel (using require
or load
directive), it enumerates all registered printers and install 它们。