如何在顶层之外打印OCaml值? [英] How can OCaml values be printed outside the toplevel?

查看:74
本文介绍了如何在顶层之外打印OCaml值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OCaml repl(顶层")可以打印任何类型的丰富内容,无论是用户定义的还是其他类型的.是否可以在顶层之外访问此功能,而不必为自己的整个类型集编写一套完全自定义的有价打印机?

The OCaml repl ("toplevel") has rich printing for any types, user-defined or otherwise. Is it possible to access this functionality outside the toplevel, rather than having to write an entirely custom set of value printers for one's own entire set of types?

推荐答案

漂亮的打印功能是顶层库的一部分.您可以在toplevel/genprintval.ml中找到源.考虑到它需要类型信息,这是可以理解的:您不能随便扔任何值,漂亮打印机的选择取决于类型.

The pretty-printing facility is part of the toplevel library. You'll find the source in toplevel/genprintval.ml. It's understandable, considering that it needs type information: you can't just throw any value at it, the choice of pretty-printer is based on the type.

如果要在程序中使用此代码,则需要链接到顶级库(toplevellib.cma)或在genprintval中进行编译(这意味着要引入足够多的类型检查器来分析类型,它可能会变得很大).

If you want to use this code in your program, you'll need to link with the toplevel library (toplevellib.cma) or compile in genprintval (which means bringing in enough bits of the type checker to analyse the type, it can get pretty big).

调试器(debugger/printval.mldebugger/loadprinter.ml)中有类似的功能(但我认为不共享代码).

There is a similar facility (but not sharing the code, I think) in the debugger (debugger/printval.ml and debugger/loadprinter.ml).

有些第三方库可以直接链接到它们,并提供漂亮的打印功能. Extlib 导出是另一种方法. 此每周骆驼新闻" 提供了更多建议.

There are third-party libraries that you can directly link against and that provide pretty-printing facilities. Extlib's Std.dump provides a very crude facility (not based on the type). Deriving by Jeremy Yallop and Jake Donham is another approach. This Caml Weekly News item offers more suggestions.

这篇关于如何在顶层之外打印OCaml值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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