View Single Post
  #3 (permalink)  
Old 10-11-2004, 06:54 PM
Martin Sandin
Guest
 
Posts: n/a
Default Re: [ocaml] Class typing question

Jacques Garrigue wrote:

> The reason is simple: class b's semantics differ from the other two.
> If you have a let expression in a class before taking any parameter,
> it is evaluated at definition time, rather than instantiation time.


Thanks, this was the fact I had missed. I actually tested for it but
must have messed up the test, concluding that the code wasn't called
until object instantiation time, being located after the 'new' operator
application, and thus unique to each object. Reality also makes this
pattern completly inappropriate for what I need.

I wanted to have instance variables that were local to the current class
declaration and not visible in subclasses. The variables are generated
by a camlp4 grammar extension, and a lot of them might be generated
(they are in fact caches for autogenerated methods), cluttering up the
namespace. And I wanted this without requiring superfluous class
parameters. This is a solution (I believe from a quick test), but could
I do this without introducing an extra class? Without doing so in the
module scope?

class ['t] a_dummy () =
let x = ref 0 in object method next = incr x; ! x end;;
class ['t] a = ['t] a_dummy ();;

# let a1 = new a;;
val a1 : 'a a = <obj>
# let a2 = new a;;
val a2 : 'a a = <obj>
# a1#next;;
- : int = 1
# a1#next;;
- : int = 2
# a2#next;;
- : int = 1


Thanks again,
Martin

Reply With Quote