After being disappointed by the responses to the hidden features of F# question, please allow me to rephrase it:
What are the lesser known, or hidden, features of OCaml that every programmer should know about?
After being disappointed by the responses to the hidden features of F# question, please allow me to rephrase it:
What are the lesser known, or hidden, features of OCaml that every programmer should know about?
There are probably a lot, since there is poor documentation. The following two are quite common in the std-lib and other external libraries. grep for them in the standard library to get a better handle of what is happening here.
noalloc
in the type definition for external functions to denote that no ocaml allocation will take place in the call.float
in the type definition for external functions allows completely unboxing the floats in the call.Something that isn't hidden, but a cool trick is using the syntactic sugar for the string module to access individual elements of a hashtbl:
(* hashtbl_syntax.ml *)
module S = String
module String = struct
let set = Hashtbl.replace
let get = Hashtbl.find
end
(* some other module *)
open Hashtbl_syntax
...
tbl.[("abc", 123)]
...
Another nice function that comes in handy is something like compose (o
) in SML/ML. You can use this to string a number of functions together while keeping spacing and order of the calls clean.
let (-->) a b = b a
You, obviously, don't have to use the arrow and can replace that with anything allowed as an infix operator in ocaml. In fact, knowing how and properly using infix operators is pretty excellent. Another great example is writing a =.
to compare floats with some amount of error.
More then that, take a look through their code in the standard library.