caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] class variables?
@ 2002-01-10 23:43 Michael Vanier
  2002-01-11 11:40 ` Warp
  2002-01-11 18:09 ` Maxence Guesdon
  0 siblings, 2 replies; 6+ messages in thread
From: Michael Vanier @ 2002-01-10 23:43 UTC (permalink / raw)
  To: caml-list


Hi,

I'm considering using ocaml for a project I'm planning.  I plan to use the
object-oriented features of ocaml extensively.  I was wondering: is there
any way to get immutable class fields that are shared by all members of the
class?  I was thinking of a list of strings, and I don't want to have to
have the list duplicated for every instance of the class.

Mike
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] class variables?
  2002-01-10 23:43 [Caml-list] class variables? Michael Vanier
@ 2002-01-11 11:40 ` Warp
  2002-01-11 18:09 ` Maxence Guesdon
  1 sibling, 0 replies; 6+ messages in thread
From: Warp @ 2002-01-11 11:40 UTC (permalink / raw)
  To: OCaml; +Cc: Michael Vanier

> Hi,
>
> I'm considering using ocaml for a project I'm planning.  I plan to use the
> object-oriented features of ocaml extensively.  I was wondering: is there
> any way to get immutable class fields that are shared by all members of
the
> class?  I was thinking of a list of strings, and I don't want to have to
> have the list duplicated for every instance of the class.

Maybe you can try using 'ref'

let strlist = ref []
...
strlist := ["a";"b";"c"] (* change inner value *)

then you can read what's "inside" strlist using !strlist

Warp

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] class variables?
  2002-01-11 18:09 ` Maxence Guesdon
@ 2002-01-11 12:19   ` Alain Frisch
  2002-01-11 12:25   ` Andreas Rossberg
  1 sibling, 0 replies; 6+ messages in thread
From: Alain Frisch @ 2002-01-11 12:19 UTC (permalink / raw)
  To: Maxence Guesdon; +Cc: Michael Vanier, caml-list

On Fri, 11 Jan 2002, Maxence Guesdon wrote:

> *But* if class foo has parameters, then the expressions before the 'objet' keyword are evaluated at
> each creation of a new instanciation, so in the following code
>
> class foo () =
>   let my_list = [ "foo" ; "bar"] in
>   object
>     ...
>   end
>
> let t = new foo
> let u = new foo
>
> t and u are two objects which don't share the list my_list.

You can write:

class foo =
  let my_list = [ "foo" ; "bar"] in
  fun () -> object
    ...
  end

in order to share my_list ...  or simply use a value defined
outside the scope of the class definition.


Alain

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] class variables?
  2002-01-11 18:09 ` Maxence Guesdon
  2002-01-11 12:19   ` Alain Frisch
@ 2002-01-11 12:25   ` Andreas Rossberg
  2002-01-11 20:39     ` Michael Vanier
  1 sibling, 1 reply; 6+ messages in thread
From: Andreas Rossberg @ 2002-01-11 12:25 UTC (permalink / raw)
  To: caml-list

Maxence Guesdon wrote:
> 
> *But* if class foo has parameters, then the expressions before the 'objet' keyword are evaluated at
> each creation of a new instanciation, so in the following code
> 
> class foo () =
>   let my_list = [ "foo" ; "bar"] in
>   object
>     ...
>   end
> 
> let t = new foo
> let u = new foo

You probably meant

	let t = new foo ()
	let u = new foo ()

> t and u are two objects which don't share the list my_list.

Right, but you can push the lambda below the let, much like for function
definitions:

	class foo =
	let my_list = [ "foo" ; "bar"] in
	fun arg ->
	object
	  ...
	end

This should do what Michael wants.

	- Andreas

-- 
Andreas Rossberg, rossberg@ps.uni-sb.de

"Computer games don't affect kids; I mean if Pac Man affected us
 as kids, we would all be running around in darkened rooms, munching
 magic pills, and listening to repetitive electronic music."
 - Kristian Wilson, Nintendo Inc.
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] class variables?
  2002-01-10 23:43 [Caml-list] class variables? Michael Vanier
  2002-01-11 11:40 ` Warp
@ 2002-01-11 18:09 ` Maxence Guesdon
  2002-01-11 12:19   ` Alain Frisch
  2002-01-11 12:25   ` Andreas Rossberg
  1 sibling, 2 replies; 6+ messages in thread
From: Maxence Guesdon @ 2002-01-11 18:09 UTC (permalink / raw)
  To: Michael Vanier; +Cc: caml-list


> Hi,
> 
> I'm considering using ocaml for a project I'm planning.  I plan to use the
> object-oriented features of ocaml extensively.  I was wondering: is there
> any way to get immutable class fields that are shared by all members of the
> class?  I was thinking of a list of strings, and I don't want to have to
> have the list duplicated for every instance of the class.
You can do it this way :
class foo =
  let my_list = [ "foo" ; "bar"] in
  object
    ...
  end

let t = new foo
let u = new foo

Now t and u are too objects sharing the same list (my_list), like anyhting before the 'object' keyword.

*But* if class foo has parameters, then the expressions before the 'objet' keyword are evaluated at
each creation of a new instanciation, so in the following code

class foo () =
  let my_list = [ "foo" ; "bar"] in
  object
    ...
  end

let t = new foo
let u = new foo

t and u are two objects which don't share the list my_list.

anybody please correct me if i'm wrong.

--
Maxence Guesdon
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] class variables?
  2002-01-11 12:25   ` Andreas Rossberg
@ 2002-01-11 20:39     ` Michael Vanier
  0 siblings, 0 replies; 6+ messages in thread
From: Michael Vanier @ 2002-01-11 20:39 UTC (permalink / raw)
  To: caml-list


Thanks to all who responded to my question about class variables.  It's a
typical case of expecting a 1-1 mapping between concepts in other
programming languages and ocaml, even when ocaml has a different (probably
better) way of achieving the same result.

Mike
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2002-01-11 21:08 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-01-10 23:43 [Caml-list] class variables? Michael Vanier
2002-01-11 11:40 ` Warp
2002-01-11 18:09 ` Maxence Guesdon
2002-01-11 12:19   ` Alain Frisch
2002-01-11 12:25   ` Andreas Rossberg
2002-01-11 20:39     ` Michael Vanier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).