Formatting your code
Instead of giving a long style guide on how to format your OCaml code,
we recommend that you use ocamlformat
, which is an auto-formatter
for OCaml code. Here's how.
Installing ocamlformat
To install ocamlformat
, type these commands into a terminal:
Then type this to verify that it has been installed:
The most recent version is 0.26.2, which is what you should see.
Configuring ocamlformat
Create a file in your src
directory called .ocamlformat
(the leading .
is important!),
with these contents:
This sets the ocamlformat
profile as the one to use
(there are a couple of others which don't concern us).
The field-space = loose
line is our personal preference;
type ocamlformat --help
to learn more about this.
Using ocamlformat
There are two ways to use ocamlformat
from the terminal command line.
The first is just to see if the new format is acceptable to you.
Say you are reformatting a file called remove_complex.ml
.
Then type:
and it will output the reformatted file on the screen.
Assuming this is acceptable to you,
and you want to permanently change the file to the reformatted one,
add the -i
argument
(-i
means "in place" since the file is modified in place):
Of course, after you've added code to this file,
you may want/need to run ocamlformat
again.
Note
Use of ocamlformat
is optional.
You're not required to use it,
but if you find that formatting is a nuisance to you,
go ahead and give it a try.
To learn more
The "home page" for ocamlformat
is
here.