Re: self in classes

From: Jerome Vouillon (vouillon@pauillac.inria.fr)
Date: Mon Feb 08 1999 - 13:21:19 MET


Date: Mon, 8 Feb 1999 13:21:19 +0100
From: Jerome Vouillon <vouillon@pauillac.inria.fr>
To: ??????? ??????? <nicholas@post.tepkom.ru>,
Subject: Re: self in classes
In-Reply-To: <Pine.LNX.4.03.9901151302590.11682-100000@post.tepkom.ru>; from ??????? ??????? on Sat, Feb 06, 1999 at 05:09:54PM +0300

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;;

-- Jιrτme



This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:19 MET