Cool! Thanks for writing this. Can you explain how defining a custom printer works? The new language features still bewilder me.
Hi,
I have made a patch for ocaml 3.12.1 which allow to have a generic
printing function. The patch is available here:
http://www.dimino.org/ocaml-3.12.1-generic-print.patch
Here is how to use it:
external show : 'a -> string = "%show"
let () = print_endline (show ([1; 2; 3], Some "foo"))
this will print:
([1; 2; 3], Some "foo")
It is also possible to define custom printers, for example:
module StringMap : sig
include Map.S with type key = string
val string_of_t : ('a -> string) -> 'a t -> string
end = struct
include Map.Make(String)
let string_of_t (type a) string_of_a (m : a t) =
show (bindings m)
end
let () = print_endline (show (StringMap.singleton "x" 1))
will print:
[("x", 1)]
Of course it is limited to what the compiler knows, for example the
following function will always returns "[<poly>; <poly>]":
let f x = show [x; x]
But i think it is already very useful for debugging.
The git repo is here:
http://www.dimino.org/gitweb/?p=ocaml-3.12.1-print.git;a=summary
Cheers,
--
Jérémie
--
Caml-list mailing list. Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs