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
Local opening of modules
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 1998-11-26 (16:08)
From: Didier Remy <remy@m...>
Subject: Re: Objects as sums
> class a = object (self)
>   method b () = ((assert false): b)
>   method c () = ((assert false): c)
> end

Here the type of method b is a (the type of objects of class a)

> and b = object (self)
>   inherit a
>   method b () = self
> end

The type of self is not the type a, since self may be an object ofa subclass
of a (imagine you are calling method b from a subclass of b).  The system
tries to unify the type of self with a, and then fails.

One solution at this point is to write class b as follows:

    class b = object (self)
      inherit a
      method b () = (self : #a :> a)

so that extra methods are hidden and self can be seem with type a.
However, it would have been better to define a as follows (which is probably
what you meant):

class a = object (self : 'a)
  method b () = ((assert false): 'a)
  method c () = ((assert false): 'a)

Here, the methods b and c return an object of the same type as their own
type. In particular, in a subclass, they will return an object of the type
of objects of the subclass...

Then, the rest of the example works unchanged.

    class c = object (self)
      inherit a
      method c () = self