From: Matt Gushee <matt@gushee.net>
To: caml-list@inria.fr
Subject: Re: Ant: [Caml-list] The "Objective" part of Objective Caml
Date: Tue, 08 Nov 2005 08:42:25 -0700 [thread overview]
Message-ID: <4370C761.9010800@gushee.net> (raw)
In-Reply-To: <Pine.LNX.4.63.0511080859590.20621@localhost.localdomain>
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
next prev parent reply other threads:[~2005-11-08 15:41 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-11-04 22:45 Florian Weimer
2005-11-07 21:41 ` Ant: [Caml-list] " Martin Chabr
2005-11-07 21:55 ` Florian Weimer
2005-11-08 1:47 ` skaller
2005-11-08 2:15 ` Brian Hurt
2005-11-08 7:15 ` Daniel Bünzli
2005-11-08 15:02 ` Brian Hurt
2005-11-08 15:39 ` Alexander Fuchs
2005-11-08 15:42 ` Matt Gushee [this message]
2005-11-08 15:56 ` Michael Wohlwend
2005-11-08 18:16 ` brogoff
2005-11-08 22:04 ` Brian Hurt
2005-11-08 23:40 ` brogoff
2005-11-09 9:00 ` skaller
2005-11-11 15:28 ` Florian Weimer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4370C761.9010800@gushee.net \
--to=matt@gushee.net \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox