English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

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: 2004-11-03 (23:33)
From: Pal-Kristian Engstad <pal_engstad@n...>
Subject: Re: [Caml-list] Self type cannot be unified with a closed object type
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:


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
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 :> 
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 :> 
class anl =
    val mutable nl = (new itv1 :> nl_vp)
    method get = nl#get
    method set i = {< nl = (nl#set i)#adapt >}
    method adapt = {< nl = nl#adapt >}

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 

Hope this helps.