有没有办法打印用户定义的数据类型?
我无法使用 print_endline
因为它需要一个字符串,而且我(认为)没有任何方法将非常简单的用户定义数据类型转换为字符串。如何检查这些数据类型的变量值?
I can't use print_endline
because it requires a string, and I don't (think) I have any way to convert my very simple user-defined datatypes to strings. How can I check the values of variables of these datatypes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在许多情况下,编写自己的 string_of_ 转换例程并不难。这是一个简单的替代方案,不需要任何额外的库或非标准 OCaml 扩展。对于我教授的使用 OCaml 的课程,这对于学生来说通常是最简单的机制。
(如果支持对字符串的通用转换,那就太好了;也许 OCaml 派生的东西会流行起来。)
In many cases, it's not hard to write your own string_of_ conversion routine. That's a simple alternative that doesn't require any extra libraries or non-standard OCaml extensions. For the courses I teach that use OCaml, this is often the simplest mechanism for students.
(It would be nice if there were support for a generic conversion to strings though; perhaps the OCaml deriving stuff will catch on.)
基本语言中没有任何内容可以为您执行此操作。有一个名为 OCaml Deriving(以 Haskell 的一项功能命名)的项目,可以自动从类型声明派生打印函数。我没用过,但听起来很棒。
http://code.google.com/p/deriving/
一旦您拥有了以下功能:打印您的类型(无论是否派生),您可以将其安装在 ocaml 顶层。这很方便,因为内置的顶级打印有时并不能完全满足您的要求。为此,请使用
#install-printer
指令,如 OCaml 手册第 9 章。There's nothing in the base language that does this for you. There is a project named OCaml Deriving (named after a feature of Haskell) that can automatically derive print functions from type declarations. I haven't used it, but it sounds excellent.
http://code.google.com/p/deriving/
Once you have a function for printing your type (derived or not), you can install it in the ocaml top-level. This can be handy, as the built-in top-level printing sometimes doesn't do quite what you want. To do this, use the
#install-printer
directive, described in Chapter 9 of the OCaml Manual.有第三方库函数,例如 OCaml Batteries Included 或 OCaml Extlib 中的
dump
,它们通常会使用它可以获得的所有运行时信息将任何值转换为字符串。但这并不能恢复所有信息;例如,构造函数名称会丢失并变成整数,因此它看起来不会完全符合您想要的方式。您基本上必须编写自己的转换函数,或者使用一些可以为您编写转换函数的工具。There are third-party library functions like
dump
in OCaml Batteries Included or OCaml Extlib, that will generically convert any value to a string using all the runtime information it can get. But this won't be able to recover all information; for example, constructor names are lost and become just integers, so it will not look exactly the way you want. You will basically have to write your own conversion functions, or use some tool that will write them for you.沿着前面的答案,ppx_sexp 是一个 PPX,用于从类型定义生成打印机。下面是一个示例,说明如何在使用 jbuilder 作为构建系统并使用 Base 和 Stdio 作为 stdlib 时使用它。
首先,jbuild 文件指定如何进行构建:
这是代码。
当您运行它时,您会得到以下输出:
S-表达式转换器存在于整个 Base 和所有相关库(Stdio、Core_kernel、Core、Async、Incremental 等)中,因此您几乎可以依靠能够序列化您在那里遇到的任何数据结构,以及您自己定义的任何数据结构。
Along the lines of previous answers, ppx_sexp is a PPX for generating printers from type definitions. Here's an example of how to use it while using jbuilder as your build system, and using Base and Stdio as your stdlib.
First, the jbuild file which specifies how to do the build:
And here's the code.
And when you run it you get this output:
S-expression converters are present throughout Base and all the related libraries (Stdio, Core_kernel, Core, Async, Incremental, etc.), and so you can pretty much count on being able to serialize any data structure you encounter there, as well as anything you define on your own.
也许超出了这个问题的范围,但也可以使用客户格式化程序
格式.printf
。考虑一个简单的例子:然后我们可以打印一个
Foo.foo
类型的值:这样做的优点是它可以很容易地用于生成字符串。
Perhaps beyond the scope of this question, but one might also use a customer formatter with
Format.printf
. Consider a simple example:And then we might print a value of type
Foo.foo
with:This has the advantage that it can readily be used to also generate a string.