caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Encoding objects using recursive first-class modules
@ 2011-02-18  6:44 Jacques Garrigue
  0 siblings, 0 replies; only message in thread
From: Jacques Garrigue @ 2011-02-18  6:44 UTC (permalink / raw)
  To: caml-list

Dear Camlers,

There has been some excitement about the possibilities of first-class
modules.
One application is to use them as objects, as is already done in the
amthing project (using interesting syntactic sugar).
This may interest some people that the relation to objects doesn't
stop at the concrete object level: combining them with recursive
modules, one can also easily encode classes, with virtual methods and
inheritance.
The basic idea is to turn classes (or rather "traits") into functors
from the final object to a list of methods.
Here is a bit of code demonstrating this.
This code is rather verbose, but using 3.13 features and amthing
syntax it can be made much lighter.

Jacques Garrigue

(* Signature for points, all methods must be functions *)
module type Point = sig
  val get_x : unit -> int
  val move : int -> unit
end

(* The Point class, with state x *)
(* Needs to be a functor, to allow creating new state *)
module PointF(X : sig end) = struct
  let x = ref 0
  let get_x () = !x
  let move d = x := !x + d
end

(* Create a new point *)
let new_point () = (module PointF(struct end) : Point)

(* Signature for the Show trait *)
module type Show = sig
  val show : unit -> string
end

(* Implementation for the Show trait *)
module ShowF(P : Point) = struct
  let show () = Printf.sprintf "x = %i" (P.get_x ())
end

(* Signature for the ShowPoint class *)
module type ShowPoint = sig include Point include Show end

(* ShowPoint class *)
module ShowPointF(P : ShowPoint) = struct
  include PointF(P)
  include ShowF(P)
end

(* Create a new ShowPoint object, by tying the knot *)
let new_showpoint () =
  let module M = struct
    module rec SP : ShowPoint = ShowPointF(SP)
  end in (module M.SP : ShowPoint)

(* Play with it *)
let sp = new_showpoint ()
let a =
  let module SP = (val sp : ShowPoint) in
  SP.move 2; SP.show ()

(* One can coerce to a supertype *)
let sp_as_p = (module (val sp : ShowPoint) : Point)


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2011-02-18  6:45 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-02-18  6:44 [Caml-list] Encoding objects using recursive first-class modules Jacques Garrigue

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).