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-01 (18:39)
From: Jerome Vouillon <Jerome.Vouillon@i...>
Subject: Re: initialization of instance variables
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

-- Jérôme