From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 7766EBB9A for ; Tue, 8 Nov 2005 17:04:25 +0100 (CET) Received: from warchicken.phauna.org (67-100-183-97.adsl.lbdsl.net [67.100.183.97]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id jA8G4N7V018783 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Tue, 8 Nov 2005 17:04:25 +0100 Received: from ogunden by warchicken.phauna.org with local (Exim 4.54) id 1EZVxN-0003lC-5m; Tue, 08 Nov 2005 11:04:21 -0500 Date: Tue, 8 Nov 2005 11:04:21 -0500 From: "N. Owen Gunden" To: caml-list@yquem.inria.fr, ocaml Subject: Re: [Caml-list] ANN: Sexplib - library for S-expression conversions Message-ID: <20051108160420.GA12718@phauna.org> Mail-Followup-To: caml-list@yquem.inria.fr, ocaml References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.8i X-Miltered: at nez-perce with ID 4370CC88.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 markus:01 mottl:01 foo:01 sexp:01 foo:01 val:01 sexp:01 val:01 baz:01 baz:01 quux:01 quux:01 printf:01 sprintf:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.1 required=5.0 tests=FORGED_RCVD_HELO autolearn=disabled version=3.0.3 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 = val t_of_sexp : Sexplib.Sexp.t -> t = # 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 = # type foo = A | B with of_sexp;; type foo = A | B val foo_of_sexp : Sexplib.Sexp.t -> foo = ---------------- 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 = # 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.foo t.bar);; val sexp_of_t : t -> Sexplib.Sexp.t = # type t' = t * [`foo|`bar] with sexp_of;; type t' = t * [ `bar | `foo ] val sexp_of_t' : t * [< `bar | `foo ] -> Sexplib.Sexp.t = # sexp_of_t' (v, `foo);; - : Sexplib.Sexp.t = ("" foo) ---------------- - O