From: boos@gr6.u-strasbg.fr (Christian Boos)
To: caml-list@pauillac.inria.fr
Cc: Thorsten Ohl <ohl@crunch.ikp.physik.th-darmstadt.de>
Subject: Re: Class variables in O'Caml??? + questions
Date: Fri, 10 May 96 14:57:11 +0200 [thread overview]
Message-ID: <9605101257.AA06291@gr6.u-strasbg.fr> (raw)
In-Reply-To: <9605101046.AA12307@crunch>
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 3171 bytes --]
Hello,
Thorsten Ohl writes:
> (...)
>
> The typical application is a class of non-uniform random number
> generators, where the distribution to be generated would be an
> instance variable, while the state of the underlying uniform
> generator should be a class variable. This way, differrent instances
> will generate different distributions, but draw from the _same_ source
> of random numbers.
>
> It is possible to emulate this with references, of course. But it
> would be somewhat unnatural ...
>
I played with O'Caml too ...
IMO, the use of references is not so unnatural. Together with structs,
it provides a clean way to encapsulate global state and actions for
classes.
I would illustrate this on a small example, a simple unique identifier
generator:
#module Identifier :
# sig
# class identifier (unit) =
# val id : int
# val mutable nb_queries : int
# method id : int
# method nb : int
# end
# end =
# struct
# let state = ref 0
#
# class identifier () =
# val mutable nb_queries = 0
# val id = let s = !state in incr state; s
#
# method id = nb_queries <- succ nb_queries; id
# method nb = nb_queries
# end
# end
#;;
module Identifier :
sig
class identifier (unit) =
val id : int
val mutable nb_queries : int
method id : int
method nb : int
end
end
#
----
By the way, I've perhaps discovered a bug, or at least an unpleasant
feature:
It seems that you can't hide the internal val's of your class
definition, when exporting its signature. What I expected is that the
previous example could be written:
#module Identifier :
# sig
# class identifier (unit) =
# method id : int
# method nb : int
# end
# end = ...
thereby hiding completely the way an identifier is implemented. But
the compiler isn't happy with that:
Class types do not match:
class identifier (unit) =
val id : int
val mutable nb_queries : int
method id : int
method nb : int
end
is not included in
class identifier (unit) =
method id : int
method nb : int
end
This is strange, since in other situations, type checking on classes
doesn't care of 'val's, as seen in the following example:
#class a () = val a = 0 method get = a end;;
class a (unit) =
val a : int
method get : int (* val a : ... *)
end
#class b () = method get = 0 end;;
class b (unit) =
method get : int (* no val *)
end
#new a () = new b ();; (* but same type !! *)
- : bool = false
But that's a minor point.
Another one is that the very interesting contribution of Jacques
Garrigue and Jun P. Furuse (labeled and optional arguments to
functions) hasn't merged with the mainstream. Will this happen one
day, at least optionaly (sort of -withlabels option) ?
Anyway, Caml is still going better and better ... That's great !
-----------------------------------------------------------------------------
- Christian Boos,
- Etudiant en Thèse d'Informatique
- http://dpt-info.u-strasbg.fr/~boos/
next prev parent reply other threads:[~1996-05-10 16:08 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
1996-05-10 10:46 Class variables in O'Caml??? Thorsten Ohl
1996-05-10 12:57 ` Christian Boos [this message]
1996-05-10 15:13 ` Class variables in O'Caml??? + questions Thorsten Ohl
1996-05-13 16:36 ` Jerome Vouillon
1996-05-13 1:06 ` Upcoming O'Labl Jacques GARRIGUE
1996-05-13 19:04 Class variables in O'Caml??? + questions David Gurr
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=9605101257.AA06291@gr6.u-strasbg.fr \
--to=boos@gr6.u-strasbg.fr \
--cc=caml-list@pauillac.inria.fr \
--cc=ohl@crunch.ikp.physik.th-darmstadt.de \
/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