Anonymous object/class (revisited)

From: David Clarke (clad@mpce.mq.edu.au)
Date: Fri Mar 12 1999 - 05:20:24 MET


Date: Fri, 12 Mar 1999 15:20:24 +1100 (EST)
From: David Clarke <clad@mpce.mq.edu.au>
To: caml-list@inria.fr
Subject: Anonymous object/class (revisited)

Hi,

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.

Dave

---------------======------------=====000000------0===-=0===-0==------0--
David Clarke "Well, you'll work harder with a gun in your back
http://www.mri.mq.edu.au/~clad for a bowl of rice a day"
---------------======------------=====000000------0===-=0===-0==------0--

----------- proto.ml --------------
(*
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

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

which is translated to

let module Mod1 = struct
    class cla2 = object
    val mutable data = 10
    method update x = data <- x
    method get = data
  end
  end
in
    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;;

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

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

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

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

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

--------- test.ml --------------
(* 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
end;;

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 =
    object
      method update x = data <- x
      method downdate x = oself#update x
    end
end;;

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