caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re: [Caml-list] Self type cannot be unified with a closed object type
@ 2004-11-04 18:22 Philippe Meunier
  0 siblings, 0 replies; 4+ messages in thread
From: Philippe Meunier @ 2004-11-04 18:22 UTC (permalink / raw)
  To: caml-list

Pal-Kristian Engstad wrote:
[...]

Tony Edgin wrote:
[...]

Jacques Garrigue wrote:
[...]

Upon further meditation your answers make a lot of sense.  Thanks guys :-)

Philippe


^ permalink raw reply	[flat|nested] 4+ messages in thread
* Self type cannot be unified with a closed object type
@ 2004-11-02 23:42 Philippe Meunier
  2004-11-03 23:31 ` [Caml-list] " Pal-Kristian Engstad
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Philippe Meunier @ 2004-11-02 23:42 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2004-11-04 18:22 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-11-04 18:22 [Caml-list] Self type cannot be unified with a closed object type Philippe Meunier
  -- strict thread matches above, loose matches on Subject: below --
2004-11-02 23:42 Philippe Meunier
2004-11-03 23:31 ` [Caml-list] " Pal-Kristian Engstad
2004-11-03 23:43 ` Tony Edgin
2004-11-04  0:17 ` Jacques Garrigue

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).