From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id D312DBCAE for ; Sat, 9 Jul 2005 05:59:33 +0200 (CEST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j693xXPf024905 for ; Sat, 9 Jul 2005 05:59:33 +0200 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id FAA20232 for ; Sat, 9 Jul 2005 05:59:32 +0200 (MET DST) Received: from kurims.kurims.kyoto-u.ac.jp (kurims.kurims.kyoto-u.ac.jp [130.54.16.1]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j693xU1l024902 for ; Sat, 9 Jul 2005 05:59:32 +0200 Received: from localhost (suiren [130.54.16.25]) by kurims.kurims.kyoto-u.ac.jp (8.13.1/8.13.1) with ESMTP id j693xSuY012419 for ; Sat, 9 Jul 2005 12:59:29 +0900 (JST) Date: Sat, 09 Jul 2005 12:59:25 +0900 (JST) Message-Id: <20050709.125925.122025999.garrigue@math.nagoya-u.ac.jp> To: caml-list@inria.fr Subject: Polymorphic map and OO syntax extension From: Jacques Garrigue X-Mailer: Mew version 4.2 on Emacs 21.3 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Miltered: at concorde with ID 42CF4BA5.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at concorde with ID 42CF4BA2.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; syntax:01 camlers:01 ocaml:01 syntax:01 syntaxes:01 val:01 mutable:01 expr:01 accessor:01 expr:01 mutable:01 accessor:01 mappings:01 ocaml:01 cmo:01 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on yquem.inria.fr X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.2 X-Spam-Level: Hello camlers, I've just put together some code bits I thought my be interesting for others. You can find them at http://www.math.nagoya-u.ac.jp/~garrigue/code/ocaml.html OO syntax extension Some camlp4 syntax extensions to write more compact code using objects. The new syntaxes are * val [mutable] x = expr with (reader|writer|accessor) generates code for an x and a set_x method, like in ruby. * obj#x <- expr generates a call to the set_x method. * {| [mutable] f1 = expr1; ...; [mutable] fn = exprn |} generates an immediate object with the above fields and the corresponding accessor methods. You may also include inherit declarations. Polymap This is a tiny module combined with a camlp4 extension, which allows you to define polymorphic mappings, where the type of the data depends on the key. (The syntax is strange, but I had no better idea.) $ ocaml camlp4o.cma pa_polymap.cmo polymap.cmo # let m = `{x=1; y=true};; val m : [> `x of int | `y of bool ] Polymap.t = # let m = `{m with z = "hello"};; val m : [> `x of int | `y of bool | `z of string ] Polymap.t = # m.`y;; - : bool = true # `{m with x = "a"};; ^ Types for tag `x are incompatible # m.`u;; Exception: Not_found. --------------------------------------------------------------------------- Jacques Garrigue Nagoya University garrigue at math.nagoya-u.ac.jp JG