Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] modules and objects
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Vincent Poirriez <Vincent.Poirriez@u...>
Subject: [Caml-list] modules and objects
Hello list,

My non-important question is "why a module is not a first class
value? while objects are."
 I was playing with modules and objects, and I found funny that it's
possible to simulate the constructor new, just by using a functor
taking an empty module. 

module Void = struct end
module type Unit = sig end

module Build(M:Unit) = struct
 let r = ref 0
 let get () = !r
 let put i = r :=i

module type CONS = sig val get: unit -> int end
module type PROD = sig val put: int -> unit end

module Foo = Build(Void)
module Bar = Build(Void)
module FooC= (Foo:CONS)
module FooP= (Foo:PROD)

What remains impossible is:
 let lm = [FooP;FooC]  impossible

While with objects:
class build = object
val mutable r = 0
method get = r
method put i = r <- i

let foo = new build
let bar = new build

class virtual cons = object
 method  virtual get : int
class virtual prod = object
 method  virtual put : int -> unit
let fooc = (foo :> cons)
let foop = (foo :> prod)

 let lo = [foo;bar]

Is correct.


tel: (33)[0]3 27 51 19 53 
Université de Valenciennes et du Hainaut-Cambrésis - Le Mont Houy
F-59313 Valenciennes cedex 9 - FRANCE
To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: