Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: "N. Owen Gunden" <ogunden@phauna.org>
To: caml-list@yquem.inria.fr, ocaml <caml-list@inria.fr>
Subject: Re: [Caml-list] ANN: Sexplib - library for S-expression conversions
Date: Tue, 8 Nov 2005 11:04:21 -0500	[thread overview]
Message-ID: <20051108160420.GA12718@phauna.org> (raw)
In-Reply-To: <f8560b80511071509x2325daefk892df1a9ecd578bf@mail.gmail.com>

On Mon, Nov 07, 2005 at 06:09:34PM -0500, Markus Mottl wrote:
> we'd like to announce the availability of "Sexplib" [...]

And here are some simple usage examples:

----------------
# open Sexplib;;

# type t = { foo : int; bar : string; } with sexp;;
type t = { foo : int; bar : string; }
val sexp_of_t : t -> Sexplib.Sexp.t = <fun>
val t_of_sexp : Sexplib.Sexp.t -> t = <fun>

# let v = { foo = 3; bar = "baz"; };;
val v : t = {foo = 3; bar = "baz"}

# sexp_of_t v;;
- : Sexplib.Sexp.t = ((foo 3) (bar baz))

# t_of_sexp (Sexp.of_string "((foo 31337) (bar \"quux\"))");;
- : t = {foo = 31337; bar = "quux"}
----------------

You can also just get a converter for a specific direction, not both:

----------------
# type foo = A | B with sexp_of;;
type foo = A | B
val sexp_of_foo : foo -> Sexplib.Sexp.t = <fun>

# type foo = A | B with of_sexp;;
type foo = A | B
val foo_of_sexp : Sexplib.Sexp.t -> foo = <fun>
----------------

A more complicated example, building a bigger type out of the same t we
used above:

----------------
# type t' = t * [`foo|`bar] with sexp_of;;
type t' = t * [ `bar | `foo ]
val sexp_of_t' : t * [< `bar | `foo ] -> Sexplib.Sexp.t = <fun>

# sexp_of_t' (v, `foo);;
- : Sexplib.Sexp.t = (((foo 3) (bar baz)) foo)
----------------

I can also manually define the converter for a type that's contained
within, and the converter for the larger type will use mine:

----------------
# let sexp_of_t t = Sexp.Atom (Printf.sprintf "<t with foo=%d and bar=%S>" t.foo t.bar);;
val sexp_of_t : t -> Sexplib.Sexp.t = <fun>

# type t' = t * [`foo|`bar] with sexp_of;;
type t' = t * [ `bar | `foo ]

val sexp_of_t' : t * [< `bar | `foo ] -> Sexplib.Sexp.t = <fun>
# sexp_of_t' (v, `foo);;
- : Sexplib.Sexp.t = ("<t with foo=3 and bar=\"baz\">" foo)
----------------

 - O


  parent reply	other threads:[~2005-11-08 16:04 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-11-07 23:09 Markus Mottl
2005-11-08 15:46 ` [Caml-list] " Florian Hars
2005-11-08 18:29   ` Markus Mottl
2005-11-09  8:42     ` skaller
2005-11-09  8:57       ` Sven Luther
2005-11-09  9:25         ` skaller
2005-11-08 16:04 ` N. Owen Gunden [this message]
2005-11-09  2:09 ` N. Owen Gunden

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=20051108160420.GA12718@phauna.org \
    --to=ogunden@phauna.org \
    --cc=caml-list@inria.fr \
    --cc=caml-list@yquem.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