Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] class variables?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Maxence Guesdon <maxence.guesdon@i...>
Subject: Re: [Caml-list] class variables?

> 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