From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 11449BC28 for ; Thu, 4 Nov 2004 01:17:45 +0100 (CET) Received: from kurims.kurims.kyoto-u.ac.jp (kurims.kurims.kyoto-u.ac.jp [130.54.16.1]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id iA40Hh6B025746 for ; Thu, 4 Nov 2004 01:17:44 +0100 Received: from localhost (suiren.kurims.kyoto-u.ac.jp [130.54.16.25]) by kurims.kurims.kyoto-u.ac.jp (8.9.3p2-20030924/3.7W) with ESMTP id JAA09858; Thu, 4 Nov 2004 09:17:37 +0900 (JST) Date: Thu, 04 Nov 2004 09:17:27 +0900 (JST) Message-Id: <20041104.091727.41193086.garrigue@math.nagoya-u.ac.jp> To: meunier@ccs.neu.edu Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Self type cannot be unified with a closed object type From: Jacques Garrigue In-Reply-To: References: X-Mailer: Mew version 4.0.64 on Emacs 21.3 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Miltered: at concorde with ID 41897527.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 ccs:01 ocaml:01 ...:98 ...:98 inherit:01 inherit:01 defining:01 jacques:01 jacques:01 coerce:01 int:01 int:01 neu:02 objects:02 X-Spam-Checker-Version: SpamAssassin 3.0.0 (2004-09-13) on yquem.inria.fr X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.0 X-Spam-Level: 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