有没有办法在ocaml中打印用户定义的数据类型? [英] Is there a way to print user-defined datatypes in ocaml?
问题描述
我不能使用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?
推荐答案
在许多情况下,编写自己的string_of_转换例程并不难.这是一个简单的替代方案,不需要任何额外的库或非标准的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.
(不过,如果支持对字符串的通用转换会很好;也许OCaml派生的东西会流行起来.)
(It would be nice if there were support for a generic conversion to strings though; perhaps the OCaml deriving stuff will catch on.)
这篇关于有没有办法在ocaml中打印用户定义的数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!