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
typing of a class
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2000-03-10 (08:08)
From: Didier Remy <remy@p...>
Subject: Re: typing of a class
David Chemouil <> writes:

> # class a (arg : a -> b) = object(self)
>     val ob = arg self
>   end
>   and b = object
>   end;;
> The instance variable self
> cannot be accessed from the definition of another instance variable

> I don't understand why it is forbidden for an object to pass itself to
> another one

Indeed, this is unsafe, since the function arg could well try to access 
the field "ob" of self that you are currently trying to initialize (so this
is not done yet).

Consider, for instance, 

    class a = object (self) val ob = self#bad method bad = ob + 1 end;;

which is very similar to:

    type a = { ob : int; }
    let rec self = { ob = self.ob + 1; };;

>  (which is possible in Java or Eiffel for example). 

Yes, but unfortunately Java is not a reference. In particular, it
initializes all fields to meaningless default values (which will often
result in a dynamically-raised null-pointer exception).