Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
initialization of instance variables
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 1999-04-06 (06:52)
From: Hendrik Tews <tews@i...>
Subject: Re: initialization of instance variables

John Whitley writes:
   While the let-bound version works, it seems to obfuscate the intent,
   which was to have an instance variable called 'x'.  It also means that
   a subclass inheriting foo' cannot reuse or redefine x.
I have two remarks about that:

1. IMO the intent of your example is that a complicated function
is computed just before object creation. I think this is best
captured by the let construction. Alternatively you could use
initializers and exploit late binding in the computation of the
complicated function.

2. You can still have an instance variable x:

   (* make initial computation _very_ explicit *)

   class foo' input =
   let x_init = complicated_function input in
   let y_init = another function x_init    in
     val x = x_init
     val y = y_init
     (* those imaginary methods again... *)