Version française
Home     About     Download     Resources     Contact us    
Browse thread
The "Objective" part of Objective Caml
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Alexander Fuchs <alexander.fuchs@u...>
Subject: Re: Ant: [Caml-list] The "Objective" part of Objective Caml
Brian Hurt wrote:
> No, the proper way to do singletons in Ocaml is with modules, not objects.

I am not sure if you meant this by 'modules, not objects', but what 
about specifying only the class type interface of the singleton, but not 
the interface of the actual singleton class in the module interface:

singleton.mli:
---------------------------
(* class signature of the singleton *)
class type foo =
object
   method print_id: unit
end

(* the singleton *)
val singleton: foo
---------------------------


singleton.ml:
---------------------------
(* copied from the interface *)
class type foo =
object
   method print_id: unit
end

(* the class of the singleton *)
class bar id =
object
   val id = id
   method print_id = print_endline (string_of_int id);
end

(* the singleton *)
let singleton =
   new bar 0
---------------------------


Now you can access:
Singleton.singleton;;

but not create (as foo is just an interface):
new Singleton.foo 1;;

and not create (as bar is not exported to the interface):
new Singleton.bar 2;;