OCaml:获取值的类型名称 [英] OCaml: get value's type name

查看:76
本文介绍了OCaml:获取值的类型名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以在OCaml中打印值的名称,例如,如果我有

Is is possible to print value's name in OCaml, for example if I have

type my_type =
  | MyType_First of int
  | MyType_Second of string

然后执行类似的操作:

let my_value = MyType_First 0 in
print_string ("my_value is of type " ^ String.from_type my_value ^ ".\n";

我可以得到"my_value的类型为MyType_First吗". ?

can I get "my_value is of type MyType_First." ?

谢谢.

推荐答案

单态解决方案:

let from_type = function
  | MyType_First _ -> "MyType_First"
  | MyType_Second _ -> "MyType_Second"

多态解决方案:无. (AFAIK,即使指定了调试标志,与构造函数相对应的词法标记也不会记录在字节码/二进制文件中.唯一可以做的就是使用一些暗的Obj.magic为构造函数打印整数标识符".)

Polymorphic solution: none. (AFAIK, lexical tokens corresponding to constructors are not recorded in the bytecode/binary, even when debugging flags are specified. The only thing one could do is to print the integer ‘identifier’ for the constructor, using some dark Obj.magic.)

这篇关于OCaml:获取值的类型名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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