How to get type information in interactive Ocaml?

假装没事ソ 提交于 2019-12-18 12:48:33

问题


I am using Ocaml of version 4. When I define interactively some type, the interpreter prints out string representation of the type immediately after that:

# type foo = Yes | No;;         <-- This is what I entered
type foo = Yes | No             <-- This is what interpreter bounced

But after I type more definitions, sometimes I want to see the text representation of the type again.

In Haskell, I could type ":t foo".

How can I do this in Ocaml?


回答1:


In utop you can use the #typeof directive:

#typeof "list";;
type 'a list = [] | :: of 'a * 'a list 

You can put values and types inside double quotes:

let t = [`Hello, `World];;
#typeof "t";;
val t : ([> `Hello ] * [> `World ]) list   

P.S. And even better solution would be to use merlin.




回答2:


As far as I know, there is actually no way in Ocaml to retrieve type information under a string form

You'll have to build a pattern matching for each of your type

type foo = Yes | No;;

let getType = function
  |Yes -> "Yes"
  |No -> "No"    
  ;;

let a = Yes;;
print_string (getType a);;


来源:https://stackoverflow.com/questions/14291326/how-to-get-type-information-in-interactive-ocaml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!