Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: meunier@ccs.neu.edu (Philippe Meunier)
To: caml-list@yquem.inria.fr
Subject: Self type cannot be unified with a closed object type
Date: Tue, 02 Nov 2004 18:42:37 -0500	[thread overview]
Message-ID: <E1CP8IP-00029f-Mt@syrma.ccs.neu.edu> (raw)

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


             reply	other threads:[~2004-11-02 23:42 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-11-02 23:42 Philippe Meunier [this message]
2004-11-03 23:31 ` [Caml-list] " Pal-Kristian Engstad
2004-11-03 23:43 ` Tony Edgin
2004-11-04  0:17 ` Jacques Garrigue

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=E1CP8IP-00029f-Mt@syrma.ccs.neu.edu \
    --to=meunier@ccs.neu.edu \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox