在OCaml中有一系列函数,如print_int,print_endline和Printf.我做不了类似的事情:
let n = 10 in
print n;; (* And I haven't to change `print` in case type of `n` changed *)
Run Code Online (Sandbox Code Playgroud)
这就是Java,C#,Python等中的多态打印.相反,我们有类似C的类型,由程序员明确定义.所以我认为OCaml在编译过程中丢失了类型信息,并且在运行时没有它,对吧?这就是为什么我们还需要mli文件的原因?
编辑:我很累写函数像*print_listi*,*print_list_tuple2i*等等.我怎样才能做得更好?
你在OCaml中用什么跟踪和调试?
为了调试我尝试了ocamldebug和Emacs插件.
对于跟踪,我希望能够打印每个变量的数据构造函数.使用Camlp4的示例如下所示:http://caml.inria.fr/pub/docs/tutorial-camlp4/tutorial007.html#toc52
type colour = Red | Green | Blue
let print_colour =
function
Red -> print_string "Red"
| Green -> print_string "Green"
| Blue -> print_string "Blue"
Run Code Online (Sandbox Code Playgroud) 是否可以在OCaml中打印值的名称,例如,如果我有
type my_type =
| MyType_First of int
| MyType_Second of string
Run Code Online (Sandbox Code Playgroud)
然后做类似的事情:
let my_value = MyType_First 0 in
print_string ("my_value is of type " ^ String.from_type my_value ^ ".\n";
Run Code Online (Sandbox Code Playgroud)
我可以得到"my_value属于MyType_First类型".?
谢谢.
我实际上正在寻找一个带有签名的函数,'a -> string
它将给定的多态类型转换为字符串.我已经想过类似的东西
let func elem = match elem with
| int -> string_of_int elem ...
Run Code Online (Sandbox Code Playgroud)
希望有人可以帮助我,谢谢!
ocaml static-typing pattern-matching parametric-polymorphism