Version franaise
Home About Download Resources Contact us
Browse thread
self in classes
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jerome Vouillon <vouillon@p...>
Subject: Re: self in classes
On Sat, Feb 06, 1999 at 05:09:54PM +0300, ??????? ??????? wrote:
> Are there any ways to call the  methods of other objects with self as an
> argument ? 
[...]
> When i'm trying to write something like that ocamlc says me that 
> self canot escape of it's definition 

If I understand right, you're trying to mutally define two classes
this way:
    class foo = object (self)
      val observers : bar list = [] 
      method register  =
        List.iter (function o -> o#register self) observers
    end and bar = object
      method register _ = ()
    end;;
This fails, because the inferred type for the method register of class
bar would be 'a -> unit, where 'a is the type of self in class
foo. But this type should not appear outside class foo.

There are two ways to solve the problem. The first is to let the
method register of class bar have a more general type, and break the
recursion between the two classes :
    class ['a] bar = object
      method register (_ : 'a) = ()
    end;;
    class foo = object (self)
      val observers : 'a bar list = [] 
      method register  =
        List.iter (function o -> o#register self ) observers
    end;;

The second way is to add a coercion of self to some fixed type:
    class foo = object (self)
      val observers : bar list = [] 
      method register  =
        List.iter (function o -> o#register (self :> < >) ) observers
    end and bar = object
      method register _ = ()
    end;;

-- Jrme