Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: meunier@ccs.neu.edu
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Self type cannot be unified with a closed object type
Date: Thu, 04 Nov 2004 09:17:27 +0900 (JST)	[thread overview]
Message-ID: <20041104.091727.41193086.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <E1CP8IP-00029f-Mt@syrma.ccs.neu.edu>

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


  parent reply	other threads:[~2004-11-04  0:17 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
2004-11-04  0:17 ` Jacques Garrigue [this message]
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=20041104.091727.41193086.garrigue@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=caml-list@yquem.inria.fr \
    --cc=meunier@ccs.neu.edu \
    /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