Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
From: Didier Remy <remy@p...>
Subject: Re: typing of a class
David Chemouil <David.Chemouil@enseeiht.fr> 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).

        Didier