From: Tony Edgin <edgin@slingshot.co.nz>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Self type cannot be unified with a closed object type
Date: Thu, 4 Nov 2004 12:43:57 +1300 [thread overview]
Message-ID: <200411041243.57603.edgin@slingshot.co.nz> (raw)
In-Reply-To: <E1CP8IP-00029f-Mt@syrma.ccs.neu.edu>
You have a small narrowing problem. Your adapt method as stated in the nl_vp
class type definition returns an object of the same type as the object the
method is activated on. This isn't what you want. You want the method to
return an object of type nl_vp but not necessarily of the same subtype as the
object being activated. To fix this, see the two changes below.
On Wed, 03 Nov 2004 12:42, Philippe Meunier wrote:
> 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 : nl_vp
> end;;
>
> class nl_v : nl_vp =
> object (self: 'a)
> method get = 0
> method set (i : int) = self
> method adapt = self
method adapt = (self :> nl_vp)
> 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
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
--
Tony Edgin
CARP
next prev parent reply other threads:[~2004-11-03 23:45 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-11-02 23:42 Philippe Meunier
2004-11-03 23:31 ` [Caml-list] " Pal-Kristian Engstad
2004-11-03 23:43 ` Tony Edgin [this message]
2004-11-04 0:17 ` Jacques Garrigue
2004-11-04 18:22 Philippe Meunier
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=200411041243.57603.edgin@slingshot.co.nz \
--to=edgin@slingshot.co.nz \
--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