General
ModuleName.(e)
is equivalent tolet open ModuleName in e
- The module
ModuleName
is opened only fore
- https://stackoverflow.com/questions/30493644/ocaml-operator
- The module
- Style guide
- http://ocamlverse.net/content/documentation_guidelines.html
- Jane street style guide is aligned with their libraries: https://opensource.janestreet.com/standards/
[@tailcall]
- Ocaml compiler does tail optimization by default.
- Use of
[@tailcall]
on a function will induce error if the function cannot be made tail recursive. - It only checks if the call is in tail position. Not the whole function.
Error will be like this:
Error (warning 51 [wrong-tailcall-expectation]): expected tailcall
Line number directives
- Starts with a
#
- These are not comments. But are ignored by ocaml lexer ??
- https://ocaml.org/manual/5.3/lex.html#sss:lex-linedir
Import only some names from a module
Unlike Haskell, no such facility is available.
But this should be good enough:
let value1, value2 = ModuleName.(value1, value2)
https://stackoverflow.com/questions/38937520/ocaml-open-only-certain-values-types-from-module
Emacs: tuareg mode
Can work with utop.
M-x run-ocaml
: start tuareg (is an alias fortuareg-run-ocaml
)C-c C-s
: Start utopC-x C-e
/C-c C-e
: Feed block of current line (or selection) to utopC-c C-t
: Show type of value under cursor
https://ocamlpro.github.io/ocaml-cheat-sheets/tuareg-mode.pdf
Tuareg: Emacs major mode for OCaml.
- Start: ??
- Eval phrase: C-x C-e or C-c C-e
- Eval buffer: C-c C-b
- Eval region: C-c C-r
- Compile: C-c C-c
- Show type of identifier (needs annotation file): C-c C-l
- Indent: C-c C-q
Insert template code snippet:
- C-c . l:
let _ in
- C-c . i:
if _ then _ else _
- C-c . m:
match _ with _
ocamlformat
An automatic code formatter.
dune integration (needs setup in dune-project):
Dry run:
dune build @fmt
Apply:
dune promote
Ignore all warnings:
dune <command> --profile release
- By default, warnings are treated like errors
- https://discuss.ocaml.org/t/disable-make-all-warnings-errors/11658
utop
- Next completion: M-right
- Previous completion: M-left
- Select completion: M-Tab
- Load a file:
#use "myfile.ml"
https://www.systutorials.com/utop-key-bindings-key-shortcuts/
ppx
- Preprocessors. Change the program before giving it to compiler.
- Similar to C macros??
Links:
- https://ocaml.org/docs/metaprogramming
- https://github.com/ocaml-ppx/ppxlib/blob/main/examples/simple-extension-rewriter
- http://ocamlverse.net/content/metaprogramming.html
- https://tarides.com/blog/2019-05-09-an-introduction-to-ocaml-ppx-ecosystem/
Eg:
- ppxderivingjsonschema: JSON schema from ocaml type
- ppxyojsonconv: JSON de/serialization
[@@deriving _]
- To provide functionality similiar to automatic typeclass derivation in haskell.
- Needs third-party libraries.
odoc
Meant to supercede an older tool?: ocamldoc
Cross referencing possible
Use comments in the
(** ‥ *)
format.Module docstring: First odoc comment
Type, exception docstrings: odoc comment right before definition (normal comments can be there in between though)
Constructor docstring: On same line
Function docstring: before and after function definition
Stop odoc mode with a 'stop comment':
(**/**)
- Another stop comment will re-enable odoc
Inlince code:
[hello]
List: ul, ol
String formatting
- {b hello}: bold
- {i hello}: italic
- {e hello}: emphasize
Alignment:
- {L hello}: left
- {C hello}: centre
- {R hello}: right