(Example) A use case of MPP in an OCaml program
Let's say you have a big variant type such as this one:
type t = A | B | C | D | E | F | G
[try]
and you want to implement a t -> string
function.
With MPP, you can write this:
{< let l = ["A"; "B"; "C"; "D"; "E"; "F"; "G";] >}
type t = {< let _ = List.iter (Printf.printf "| %s ") l >}
let to_string = function
{< let _ = List.iter (fun e -> Printf.printf "| %s -> %S" e e) l >}
and then you have that:
type t = | A | B | C | D | E | F | G
let to_string = function
| A -> "A"
| B -> "B"
| C -> "C"
| D -> "D"
| E -> "E"
| F -> "F"
| G -> "G"
[try]
As you may see, if you grow the type t
by adding more cases, you don't have to
manually update the function to_string
.
started on 2013-10-04 13:31:30+00:00, (re)generated on 2014-01-15 15:14:11+00:00