From: "Jérémie Dimino" <jeremie@dimino.org>
To: Alex Rubinsteyn <alex.rubinsteyn@gmail.com>
Cc: Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] Generic printer patch
Date: Wed, 07 Dec 2011 18:10:58 +0100 [thread overview]
Message-ID: <1323277858.32238.30.camel@arrakis> (raw)
In-Reply-To: <CAMB58pGmj6K6U1S79-WEx+k1g=Y+d6=Y4HM9nMxEixUFPkN22w@mail.gmail.com>
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 = "<id=" ^ string_of_int 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
next prev parent reply other threads:[~2011-12-07 17:11 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-12-07 15:28 Jérémie Dimino
2011-12-07 16:46 ` Alex Rubinsteyn
2011-12-07 17:10 ` Jérémie Dimino [this message]
2011-12-07 16:56 ` François Bobot
2011-12-07 17:34 ` Jérémie Dimino
2011-12-08 12:00 ` Romain Bardou
2011-12-08 14:21 ` Gerd Stolpmann
2011-12-08 18:20 ` Martin Jambon
2011-12-08 21:39 ` Gabriel Scherer
2011-12-09 7:22 ` Jérémie Dimino
2011-12-09 9:26 ` Gabriel Scherer
2011-12-09 7:15 ` Jérémie Dimino
2011-12-08 2:33 ` Edgar Friendly
2011-12-08 7:28 ` François Bobot
2011-12-08 9:00 ` Alain Frisch
2011-12-08 9:24 ` Gerd Stolpmann
2011-12-08 10:32 ` Alain Frisch
2011-12-08 10:41 ` Jonathan Protzenko
2011-12-08 12:00 ` Philippe Veber
2011-12-08 5:26 ` Martin Jambon
2011-12-08 6:52 ` Jérémie Dimino
2011-12-08 7:44 ` Martin Jambon
2011-12-08 9:37 ` Jérémie Dimino
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1323277858.32238.30.camel@arrakis \
--to=jeremie@dimino.org \
--cc=alex.rubinsteyn@gmail.com \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox