English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Polymorphic map and OO syntax extension
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-07-09 (03:59)
From: Jacques Garrigue <garrigue@m...>
Subject: Polymorphic map and OO syntax extension
Hello camlers,

I've just put together some code bits I thought my be interesting for

You can find them at

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

    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
		   <A HREF=http://www.math.nagoya-u.ac.jp/~garrigue/>JG</A>