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: meunier@c...
Subject: Self type cannot be unified with a closed object type
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