OCaml 顶级输出格式 [英] OCaml toplevel output formatting

查看:62
本文介绍了OCaml 顶级输出格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在 OCaml 的顶层执行以下操作:

If I execute the following in OCaml's toplevel:

#require "num";;
open Ratio;;

ratio_of_int 2;;

输出为:

- : Ratio.ratio = <ratio 2/1>

这样的格式怎么可能?来源告诉我 Ratio.ratio 是一个记录.所以输出应该更类似于

How's a formatting like this possible? The sources tell me that Ratio.ratio is a record. So the output should be more akin to

{numerator = <big_int 2>; denominator = <big_int 1>; normalized = true}

我尝试查看比率输出是否以某种方式在顶层进行了硬编码,但此搜索没有结果.作为 OCaml 的新手,我必须问我是否遗漏了一些重要的东西?在重载字符串化函数的语言中,这并不奇怪,但在 OCaml 的情况下,我发现这种行为很不合适.

I tried see if ratio output is somehow hardcoded in toplevel, but this search was fruitless. Being new to OCaml, I must ask if I'm missing something important? In a language that has overloaded stringification funcs this wouldn't be strange, but in OCaml's case I find this behavior quite out of place.

推荐答案

Findlib 有一个漂亮的打印机,专门用于 ratio 模块.它没有打印出 <abstr>(接口不公开记录),而是打印出您看到的内容.如果你想看看,看看 findlib/num_top_printers.ml:

Findlib has a pretty printer specifically for the ratio module. Instead of printing out <abstr> (the interface doesn't expose the record), it prints out what you saw. If you want to check it out, look at findlib/num_top_printers.ml:

let ratio_printer fmt v =
  Format.fprintf fmt "<ratio %s>" (Ratio.string_of_ratio v)

这篇关于OCaml 顶级输出格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆