caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* 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; 5+ 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] 5+ messages in thread

* Re: [Caml-list] Self type cannot be unified with a closed object type
  2004-11-02 23:42 Self type cannot be unified with a closed object type Philippe Meunier
@ 2004-11-03 23:31 ` Pal-Kristian Engstad
  2004-11-03 23:43 ` Tony Edgin
  2004-11-04  0:17 ` Jacques Garrigue
  2 siblings, 0 replies; 5+ messages in thread
From: Pal-Kristian Engstad @ 2004-11-03 23:31 UTC (permalink / raw)
  To: caml-list

On Tuesday 02 November 2004 03:42 pm, 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:

Hi, 

The state pattern is useful in OO languages since switches aren't safe. 
However, I will assume that you do need to extend the state class, so I won't 
go on about how you can use union types. 

Your only problem is that the 'self' type is extendible. Therefore, while 
OCaml parses your recursive structure, there is no specific closed type of 
self. If the type of 'self' was closed, then you could not extend your class.

The easiest solution in your case is to make adapt return a closed type, 
namely your nl_vp class type. Simply change "method adapt : 'a" to "method 
adapt: nl_vp": 

type itv2stub = { mutable ival : int }

let new_itv2_stub () = { ival = 0 }
let get_itv2_stub s = s.ival
let set_itv2_stub s v = (s.ival <- v; s)

class type nl_vp = object
    method get : int
    method set : int -> nl_vp
    method adapt : nl_vp
  end;;
 
class itv1 : nl_vp =
  object (self)
    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 : nl_vp =
  object (self)
    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;;

By the way, notice that {< itv1_val = i >} creates a _new_ object, hence it 
might not do what you expect. I.e. the "mutable" keyword is not nescessary in 
itv1.

Hope this helps.

PKE.


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

* Re: [Caml-list] Self type cannot be unified with a closed object type
  2004-11-02 23:42 Self type cannot be unified with a closed object type 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
  2 siblings, 0 replies; 5+ messages in thread
From: Tony Edgin @ 2004-11-03 23:43 UTC (permalink / raw)
  To: caml-list

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


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

* Re: [Caml-list] Self type cannot be unified with a closed object type
  2004-11-02 23:42 Self type cannot be unified with a closed object type 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
  2 siblings, 0 replies; 5+ messages in thread
From: Jacques Garrigue @ 2004-11-04  0:17 UTC (permalink / raw)
  To: meunier; +Cc: caml-list

From: meunier@ccs.neu.edu (Philippe Meunier)

> 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 = ...
>  
> class itv1 =
>   object (self)
>     inherit nl_v
...
>     method adapt = if self#get = 2 then ((new itv2) :> nl_vp) else (self :> nl_vp)
>   end
> and itv2 =
>   object (self)
>     inherit nl_v
...
>     method adapt = if self#get = 7 then ((new itv1) :> nl_vp) else (self :> nl_vp)
>   end;;

The problem is clear enough: the method adapt in nl_vp has the type of
self, while in itv1 and itv2 you want it to have the type nl_vp.
The "type of self" is not the type of the class you are currently
defining, but the type of any class that is going to extend it, so
they are not compatible.

The type in nl_vp should be
    method adapt : nl_vp
and then you have no problem.

Jacques Garrigue


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

* Re: [Caml-list] Self type cannot be unified with a closed object type
@ 2004-11-04 18:22 Philippe Meunier
  0 siblings, 0 replies; 5+ 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] 5+ messages in thread

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

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-11-02 23:42 Self type cannot be unified with a closed object type 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
2004-11-04 18:22 Philippe Meunier

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).