The 6 parameters of (’a, ’b, ’c, ’d, ’e, ’f) format6
- April 18, 2014
The infamous format6
type is the basis of the hackish
but damn convenient, type-safe way in which OCaml handles
format strings:
Printf.printf "%d) %s -> %f\n"
3 "foo" 5.12
The first argument of printf
in this example is not a
string, but a format string (they share the same literal syntax, but
have different type, and there is a small hack in the type-inference
engine to make this possible). It’s type, which you can get by asking
("%d) %s -> %f\n" : (_,_,_,_,_,_) format6)
in a
toplevel, is a mouthful (and I renamed the variables for better
readability):
(int -> string -> float -> 'f, 'b, 'c, 'd, 'd, 'f) format6
What do those six arcane parameters mean? In the process of reviewing Benoît Vaugon’s work on using GADTs to re-implement format functions, I’ve finally felt the need (after years of delighted oblivion) to understand each of those parameters. And that came after several days of hacking on easier-to-understand ten-parameters GADT functions; hopefully most of our readers will never need this information, but the probability that I need to refresh my memory in the future is strong enough for me to write this blog post.
OCaml 5.00
- April 1, 2014