ocaml cheatsheet
Notes from when I went on Dillon Mulroy's Twitch stream to talk about getting started with OCaml.
learning
The official "Learn OCaml" docs are always improving, but here are some other resources I found invaluable when starting out.
Tools & techniques
Programming language design & theory
👉 OCaml Programming: Correct + Efficient + Beautiful
project setup
dune init proj <name>
cd <name>
opam switch create . --deps-only
Minimal dune-project
for CLIs:
(lang dune 3.11)
(generate_opam_files true)
(package
(name <name of your cli>)
(depends ocaml dune))
git
Initialize a git repo:
git init
Then add a .gitignore
:
_build/
_opam/
opam switch
Setup opam
to auto-enable the switch when entering the directory:
opam init --enable-shell-hook
Or do this every time:
eval $(opam env)
dev dependencies
opam install ocaml-lsp-server odoc ocamlformat utop
neovim
Do not do the rtp
thing that opam
suggests.
Instead, do this if using lazy.nvim
as your package manager:
-- ~/.config/nvim/lua/plugins/ocaml.lua
return {
{
"neovim/nvim-lspconfig",
opts = {
servers = {
-- use local lsp installed within project's opam switch
ocamllsp = { mason = false },
},
},
},
{
"nvim-treesitter/nvim-treesitter",
opts = function(_, opts)
if type(opts.ensure_installed) == "table" then
vim.list_extend(opts.ensure_installed, { "ocaml", "ocaml_interface" })
end
end,
},
}