From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 80425BC29 for ; Wed, 3 Nov 2004 00:42:39 +0100 (CET) Received: from amber.ccs.neu.edu (amber.ccs.neu.edu [129.10.116.51]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id iA2NgcZg026834 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Wed, 3 Nov 2004 00:42:39 +0100 Received: from syrma.ccs.neu.edu ([129.10.117.168]) by amber.ccs.neu.edu with esmtp (TLSv1:DHE-RSA-AES256-SHA:256) (Exim 4.34) id 1CP8IP-0007Gk-UC for caml-list@yquem.inria.fr; Tue, 02 Nov 2004 18:42:38 -0500 Received: from meunier by syrma.ccs.neu.edu with local (Exim 4.34) id 1CP8IP-00029f-Mt for caml-list@yquem.inria.fr; Tue, 02 Nov 2004 18:42:37 -0500 From: meunier@ccs.neu.edu (Philippe Meunier) To: caml-list@yquem.inria.fr Subject: Self type cannot be unified with a closed object type Message-Id: Sender: Philippe Meunier Date: Tue, 02 Nov 2004 18:42:37 -0500 X-Miltered: at nez-perce with ID 41881B6E.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; ccs:01 ocaml:01 val:01 mutable:01 val:01 mutable:01 runtime:01 checker:01 foo:01 conceptually:01 ...:98 stub:01 stub:01 expression:01 expression:01 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: Hi, 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 = object (self: 'a) method get = 0 method set (i : int) = self method adapt = self end;; type itv2_ptr;; external new_itv2_stub : unit -> itv2_ptr = "new_itv2_stub";; external get_itv2_stub : itv2_ptr -> int = "get_itv2_stub";; external set_itv2_stub : itv2_ptr -> int -> itv2_ptr = "set_itv2_stub";; class itv1 = object (self) inherit nl_v val mutable itv1_val = ~-1 method get = itv1_val method set i = {< itv1_val = i >} method adapt = if self#get = 2 then ((new itv2) :> nl_vp) else (self :> nl_vp) end and itv2 = object (self) inherit nl_v val mutable itv2_val = new_itv2_stub () method get = get_itv2_stub itv2_val method set i = {< itv2_val = set_itv2_stub itv2_val i >} method adapt = if self#get = 7 then ((new itv1) :> nl_vp) else (self :> nl_vp) end;; class anl = object val mutable nl = (new itv1 :> nl_vp) method get = nl#get method set i = {< nl = (nl#set i)#adapt >} method adapt = {< nl = nl#adapt >} end;; (**************************************************) The problem is that I need to be able to switch dynamically between states itv1 and itv2 depending on some runtime condition. Hence the "if "expression in the "adapt" method of both itv1 and itv2. But when I try that I get the following error message from the type checker, complaining about such "if" expression: File "foo.ml", line 30, characters 19-82: This expression has type nl_vp but is here used with type < adapt : 'a; get : int; set : int -> 'a; .. > as 'a Self type cannot be unified with a closed object type As you can see I tried using a class type nl_vp as suggested towards the end of section 3.12 of the manual but so far it hasn't done me any good. So I'm stuck. Conceptually the thing I'm trying to do is quite simple so I think I'm just missing something obvious but I can't figure out what. I'd really appreciate if someone could tell me how to get this thing working... Thanks a lot, Philippe