From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id pB7HB4K4006957 for ; Wed, 7 Dec 2011 18:11:04 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AmoFAIqd307VuiYS/2dsb2JhbABDhDZQoxmCNYEFgXIBAQUMFwRSEAkCGgIVAQEPAgIsKwaIHKU6kVcUgSCGdQSBcYEWBKcP X-IronPort-AV: E=Sophos;i="4.71,314,1320620400"; d="scan'208";a="122453745" Received: from solaria.dimino.org ([213.186.38.18]) by mail4-smtp-sop.national.inria.fr with ESMTP/TLS/ADH-AES256-SHA; 07 Dec 2011 18:10:59 +0100 Received: from arrakis (localhost.localdomain [127.0.0.1]) by solaria.dimino.org (Postfix) with ESMTP id E3AE780088; Wed, 7 Dec 2011 18:10:58 +0100 (CET) Received: by arrakis (Postfix, from userid 1000) id 760CF11F2D4; Wed, 7 Dec 2011 18:10:58 +0100 (CET) Message-ID: <1323277858.32238.30.camel@arrakis> From: =?ISO-8859-1?Q?J=E9r=E9mie?= Dimino To: Alex Rubinsteyn Cc: Caml List Date: Wed, 07 Dec 2011 18:10:58 +0100 In-Reply-To: References: <1323271707.32238.17.camel@arrakis> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.0.3-3 Mime-Version: 1.0 Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by walapai.inria.fr id pB7HB4K4006957 Subject: Re: [Caml-list] Generic printer patch Le mercredi 07 décembre 2011 à 11:46 -0500, Alex Rubinsteyn a écrit : > Cool! Thanks for writing this. Can you explain how defining a custom > printer works? The new language features still bewilder me. If you want to define a custom printer for a type named foo, you have to define a function named string_of_foo. It must be in the same module as foo. It takes as arguments printers for its type variables, the value to print and returns a string. For example, if your type is: type ('a, 'b) foo a custom printer for foo will have the signature: val string_of_foo : ('a -> string) -> ('b -> string) -> ('a, 'b) foo -> string For example: module Id : sig type t val string_of_t : t -> string val create : unit -> t end = struct type t = int let string_of_t x = "" let next = ref 0 let create () = incr next; !next end You may also want to use "show" in your custom printer. In order to tell show to use the printers given as arguments, you must use type parameters like that: let string_of_foo (type a) (type b) string_of_a string_of_b (x : (a, b) foo) = ... For example to export a generated printer for an abstract type: module M : sig type ('a, 'b) t val string_of_t : ('a -> string) -> ('b -> string) -> ('a, 'b) t -> string end = struct type ('a, 'b) t = A of 'a | B of 'b let string_of_t (type a) (type b) string_of_a string_of_b (x : (a, b) t) = show x end Cheers, -- Jérémie