From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id D36FEBB9A for ; Tue, 8 Nov 2005 16:41:53 +0100 (CET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id jA8Ffrsc020816 for ; Tue, 8 Nov 2005 16:41:53 +0100 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id QAA09624 for ; Tue, 8 Nov 2005 16:41:52 +0100 (MET) Received: from mz1.forethought.net (mzpi3.forethought.net [216.241.36.12]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id jA8FfpiD020805 (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NO) for ; Tue, 8 Nov 2005 16:41:52 +0100 Received: from [216.241.35.41] (helo=[10.0.0.2]) by mz1.forethought.net with esmtp (Exim 4.51) id 1EZVbT-0008Cd-6u for caml-list@inria.fr; Tue, 08 Nov 2005 08:41:46 -0700 Message-ID: <4370C761.9010800@gushee.net> Date: Tue, 08 Nov 2005 08:42:25 -0700 From: Matt Gushee User-Agent: Mozilla Thunderbird 1.0.7 (X11/20051002) X-Accept-Language: en-us, en MIME-Version: 1.0 To: caml-list@inria.fr Subject: Re: Ant: [Caml-list] The "Objective" part of Objective Caml References: <20051107214110.46596.qmail@web26805.mail.ukl.yahoo.com> <87acggxguw.fsf@mid.deneb.enyo.de> <1131414473.23991.37.camel@rosella> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Miltered: at concorde with ID 4370C741.002 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at concorde with ID 4370C73F.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 allocating:01 failwith:01 foo:01 mli:01 val:01 foo:01 ocaml:01 factory:98 factory:98 ...:98 ....:98 ....:98 wrote:01 modules:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 Brian Hurt wrote: >> Don't you have immediate objects [1] to do singletons ? > > > No- because immediate objects still allocate a new object every time > they are evaluated. I suppose you could do: > > let factory = > let myobject = new myclass in > fun () -> myobject > ;; > > and do a factory method, but there is no way (that I know of) to prevent > someone else from doing: > let myotherobject = new myclass > and allocating a different object of the same class. Just for amusement, here are two ways: [1] let factory = let instance_exists = ref false in let mkobj () = object initializer if !instance_exists then failwith "Sorry." else instance_exists := true ... end in mkobj [2] foo.mli: -------- class type t_myclass = object .... end val factory : unit -> t_myclass foo.ml: ------- class type t_myclass = object .... end class myclass = object .... end let instance : t_myclass option ref = ref None let factory () = match !instance with | Some i -> i | None -> let i = new myclass in instance := Some i; i > No, the proper way to do singletons in Ocaml is with modules, not objects. But Brian is right, I'm sure. -- Matt Gushee Englewood, CO, USA