Anonymous object/class (revisited)

From: David Clarke (
Date: Fri Mar 12 1999 - 05:20:24 MET

Date: Fri, 12 Mar 1999 15:20:24 +1100 (EST)
From: David Clarke <>
Subject: Anonymous object/class (revisited)


The following camlp4 preprocessor code allows you to use
anonymous objects in ocaml, including objects inside
classes and other anonymous objects.

It is a slight improvement on William Chester's implementation
from less than a month ago. It also contains a little more
documentation and an example.

The code transforms an anonymous object declaration into a
class defined within a local module, and then creates an instance
of that class. Simple.

The code follows (with an example in the comment).

If you find this useful, implement any extensions, find some
problems or limitations, etc etc, I would love to know.


David Clarke "Well, you'll work harder with a gun in your back for a bowl of rice a day"

----------- --------------
CAMLP4 code extending OCAML with anonymous objects.

An anonymous object can appear anywhere an expression can, including
inside classes and other anonymous objects.

An example is

    val mutable data = 10
    method update x = data <- x
    method get = data

which is translated to

let module Mod1 = struct
    class cla2 = object
    val mutable data = 10
    method update x = data <- x
    method get = data
    new Mod1.cla2

The syntax of anonymous objects is the same as the
  object ... end
clause of the class-expr clause in the grammar.

Limitations: There are bound to be some.


let genname =
  let count = ref 0 in
    function pre -> count := !count + 1; pre ^ string_of_int(!count);;

open Pcaml;;

  expr: LAST
    [[ ce = class_expr ->
         let modname = genname "Mod" in
         let cname = genname "cla" in
         let longname = [modname; cname] in
           <:expr< let module $modname$ =
                         class $cname$ = $ce$;
                       in new $list:longname$ >>

-------- instructions -----------
Needs camlp4.

To compile:
ocamlc -pp "camlp4o pa_extend.cmo q_MLast.cmo" -I `camlp4 -where` -c

To run just preprocessor:
camlp4o pr_o.cmo ./proto.cmo

To apply to your own programs:
ocamlc -pp "camlp4o ./proto.cmo" -o tester

--------- --------------
(* this is a test file for the anonymous objects extension to OCAML *)

(* a simple example *)
let z = object
    val mutable data = 10
    method update x = data <- x
    method get = data

let y = z#get;;

let _ = print_int y; print_newline (); flush stdout;;

(* an example with an lexically scoped inner object *)
let outer =
object (oself)
  val mutable data = "hello"
  method print = print_string data; print_newline ()
  method update x = data <- x
  method extern =
      method update x = data <- x
      method downdate x = oself#update x

let _ = outer#print;;
let _ = outer#update "spam"; outer#print;;
let ex = outer#extern;;
let _ = ex#update "jimmy jo"; outer#print;;
let _ = ex#downdate "flavour of the month"; outer#print; flush stdout;;

------- EOF ----------

This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:20 MET