Reputation: 31
Is there a way to know what type of something is in Ocaml when you enter it in ocaml.
For example, in python, if you put: "type(3)" you got int. Another example, if you put: type("hello") you got string.
What is the equivalent in ocaml?
Upvotes: 0
Views: 174
Reputation: 66808
Type information is not present in compiled OCaml code. So there's no function you can call to get the type of a value.
Note that the type of a given value in OCaml is fixed, i.e., it can't change. So there's no real need to test types while the code is running. The type at compile time is the permanent type of the value.
The OCaml REPL (often called "toplevel") does have type information. If you type a value at the prompt, it will tell you the type:
# 3;;
- : int = 3
# "hello";;
- : string = "hello"
#
If you have code in a file "mycode.ml" you can load it into the REPL and the interpreter will print the types of all the top-level names:
# #use "mycode.ml";;
val f : int -> int = <fun>
These are very simple solutions. You can also set yourself up with an IDE that shows the type of whatever is under the cursor as you edit your code.
Upvotes: 3