From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: caml-list@inria.fr
Subject: Polymorphic map and OO syntax extension
Date: Sat, 09 Jul 2005 12:59:25 +0900 (JST) [thread overview]
Message-ID: <20050709.125925.122025999.garrigue@math.nagoya-u.ac.jp> (raw)
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>
next reply other threads:[~2005-07-09 3:59 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-07-09 3:59 Jacques Garrigue [this message]
2005-07-15 1:41 ` [Caml-list] " Marc Lasson
2005-07-15 2:40 ` Jacques Garrigue
2005-07-15 4:10 ` Martin Jambon
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=20050709.125925.122025999.garrigue@math.nagoya-u.ac.jp \
--to=garrigue@math.nagoya-u.ac.jp \
--cc=caml-list@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