问题
I'm a OCaml newbie working with some pretty complex (at least for me) OCaml code I didn't write. It would help a lot to understand it if I could see the inferred types for some of the values, as I can do with F# and Visual Studio by hovering over any value, e.g.:
(screenshot borrowed from http://theburningmonk.com/2010/01/learning-f-part-1/)
I guess I could break down the code and feed it to the toplevel to get the types, but is there any other, simpler way?
回答1:
First, you must compile your file (foo.ml) with the -annot flag. This generates the annotation file (foo.annot) that contains type information about all identifiers inside. You can include this as part of your makefile or build process.
Then, you need to rely on your IDE to display that information. I do this in Tuareg (the emacs mode) using Ctrl+C, Ctrl+T, which highlights the current expression and displays its type.
回答2:
OcaIDE which is an O'Caml plug-in for Eclipse displays the inferred types on the fly while editing your code.
Example:
回答3:
You could enter the code into the ocaml toplevel. Yeah, it's not as pretty, but it'll show the types.
回答4:
What works for me when reading OCaml code is to begin with the .mli files to see what the overall structure of the project is. From there, and having an idea of what are the dependencies, you can explore from the toplevel.
Alternatively, you can ocamlc -i the file in question for an overview of all the declared types and values, or use Tuareg mode in Emacs as replied elsewhere, if you are of that persuasion.
来源:https://stackoverflow.com/questions/5981620/see-inferred-types-in-complex-ocaml-code