caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* initialization of instance variables
@ 1999-03-27 18:04 John Whitley
  1999-04-01 13:10 ` Jerome Vouillon
  0 siblings, 1 reply; 4+ messages in thread
From: John Whitley @ 1999-03-27 18:04 UTC (permalink / raw)
  To: caml-list


I'm curious as to why class instance variable definitions cannot be
used in following instance variable definitions, only in methods and
initializers.  Is this an implmentation-driven restriction, or is
there some more subtle semantics-based reason to prevent this?

-- John




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

* Re: initialization of instance variables
  1999-03-27 18:04 initialization of instance variables John Whitley
@ 1999-04-01 13:10 ` Jerome Vouillon
  1999-04-01 15:01   ` John Whitley
  0 siblings, 1 reply; 4+ messages in thread
From: Jerome Vouillon @ 1999-04-01 13:10 UTC (permalink / raw)
  To: John Whitley, caml-list

On Sat, Mar 27, 1999 at 01:04:33PM -0500, John Whitley wrote:
> 
> I'm curious as to why class instance variable definitions cannot be
> used in following instance variable definitions, only in methods and
> initializers.  Is this an implmentation-driven restriction, or is
> there some more subtle semantics-based reason to prevent this?

It would actually be quite easy to allow the use of an instance
variable in the following instance variable definitions.  The reason
it is not allowed for the moment is that I think it may be a bit
confusing.  Indeed, an identifier "x" in a method definition stands
for the current value of the instance variable "x" whereas, in an
instance variable definition, it would stand for the initial
definition of the instance variable.

For instance, if "o" is an object of the class "c" below, the
expression "o#get_y ()" will always evaluate to 1, whereas the
expression "o#get_x ()" will evaluate to 2 if the method "set" was
invoked previously.

    class c = object
      val mutable x = 1
      method set = x <- 2
      method get_x () = x
      val y = fun () -> x
      method get_y = y
    end

-- Jérôme




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

* Re: initialization of instance variables
  1999-04-01 13:10 ` Jerome Vouillon
@ 1999-04-01 15:01   ` John Whitley
  1999-04-03 10:40     ` Hendrik Tews
  0 siblings, 1 reply; 4+ messages in thread
From: John Whitley @ 1999-04-01 15:01 UTC (permalink / raw)
  To: caml-list

Jerome Vouillon writes:
 > It would actually be quite easy to allow the use of an instance
 > variable in the following instance variable definitions.  The reason
 > it is not allowed for the moment is that I think it may be a bit
 > confusing. [discussion elided]

Ah, indeed!  That hadn't occured to me offhand, probably as the
code that elicited this question uses immutable instance variables.

In my code, the alternative of using let bindings seemed
counter-intuitive:

(* prohibited form *)
class foo input =
object
  val x = complicated_function input
  val y = another_function x
  (* imagine some methods... *)
end

(* a working, let-bound form *)
class foo' input =
let x = complicated_function input in
object
  val y = another function x
  (* those imaginary methods again... *)
end


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.


Thanks much,
John




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

* Re: initialization of instance variables
  1999-04-01 15:01   ` John Whitley
@ 1999-04-03 10:40     ` Hendrik Tews
  0 siblings, 0 replies; 4+ messages in thread
From: Hendrik Tews @ 1999-04-03 10:40 UTC (permalink / raw)
  To: caml-list


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
   object
     val x = x_init
     val y = y_init
     (* those imaginary methods again... *)
   end


Bye,

Hendrik




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

end of thread, other threads:[~1999-04-06  6:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-03-27 18:04 initialization of instance variables John Whitley
1999-04-01 13:10 ` Jerome Vouillon
1999-04-01 15:01   ` John Whitley
1999-04-03 10:40     ` Hendrik Tews

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).