Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] prevent more than one object
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Nick Name <nick.name@i...>
Subject: Re: [Caml-list] prevent more than one object
On Wed, 02 Jul 2003 18:14:23 +0200
Mirko Aigner <Mirko.Aigner@alcatel.de> wrote:

> 
>  The problem is the class is still public and instances can be build,
>  any way to prevent this ?!

I would make the class private in the module and supply only access
functions; if you need it to be a class you should expose only the class
type, but there is Oo.copy so your value will never be granted unique.

Try this

module M : sig
  class type singleton = object
    method f : int
  end

  val x : singleton
end = 
struct
  class singleton = object
    method f = 0
  end

  let x = new singleton
end

You can't create a new singleton with "new" but still can do that with
Oo.copy M.x. It is better to completely hide the type if you can, as in

module M : sig
  type singleton

  val x : singleton
end = struct ...

Vincenzo

-- 
Ho dato al mio dolore la forma di parole abusate
che mi prometto di non pronunciare mai più
[CSI]

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners