Browse thread
Polymorphic map and OO syntax extension
- Jacques Garrigue
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| 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
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
<A HREF=http://www.math.nagoya-u.ac.jp/~garrigue/>JG</A>