Version française
Home     About     Download     Resources     Contact us    
Browse thread
Self type cannot be unified with a closed object type
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] Self type cannot be unified with a closed object type
From: meunier@ccs.neu.edu (Philippe Meunier)

> I'm trying to implement the state pattern in Ocaml and I'm looking for
> help.  I have two classes itv1 and itv2 representing two kinds of
> state.  I'm trying to coerce objects from these two classes to a
> single class nl_v so I can use them indefferently inside objects of
> the class anl:
> 
> class type nl_vp =
>   object ('a)
>     method get : int
>     method set : int -> 'a
>     method adapt : 'a
>   end;;
>  
> class nl_v : nl_vp = ...
>  
> class itv1 =
>   object (self)
>     inherit nl_v
...
>     method adapt = if self#get = 2 then ((new itv2) :> nl_vp) else (self :> nl_vp)
>   end
> and itv2 =
>   object (self)
>     inherit nl_v
...
>     method adapt = if self#get = 7 then ((new itv1) :> nl_vp) else (self :> nl_vp)
>   end;;

The problem is clear enough: the method adapt in nl_vp has the type of
self, while in itv1 and itv2 you want it to have the type nl_vp.
The "type of self" is not the type of the class you are currently
defining, but the type of any class that is going to extend it, so
they are not compatible.

The type in nl_vp should be
    method adapt : nl_vp
and then you have no problem.

Jacques Garrigue