Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Jerome Vouillon <vouillon@clipper.ens.fr>
To: Andrew Conway <arc@sequence.stanford.edu>
Cc: caml-list@pauillac.inria.fr
Subject: Re: Closed Objects
Date: Fri, 13 Sep 1996 14:31:50 +0200 (MET DST)	[thread overview]
Message-ID: <Pine.3.89.9609131323.A21177-0100000@vedette> (raw)
In-Reply-To: <199609121922.MAA19250@vegemite.Stanford.EDU>



> class virtual runaround () as self : 'a =
>     virtual null : bool
>     virtual next : runaround
>     virtual myself : runaround 
> end;;
> 
> (* Add a method. This DOESN'T compile in ocaml 1.01, but 
>    did compile in ocaml 1.00. The error message says that it
>    is closed, but not marked closed. 
> 
>    I don't want it to be closed. Is it impossible to return "self"
>    in a non-closed class?
> 
>  *)
> 
> class virtual run2 () as self =
>     inherit runaround ()
>     method myself = (self :> runaround)
> end;;

Coercion operator ( e :> t ) is not always general enough. What it does is
guess a type t' subtype of t independant of expression e, and then checks
the type of expression e is an instance of type t'. For instance, if
t = <x : int>, then t' = <x : int; ..>. In this case, any subtype of t is
an instance of type t'. But this is not always the case. Consider type
t = <x : 'a> as 'a. The types <x : 'b; y : int> as 'b and
<x : t; y : int> are both subtypes of this type, but they are not
instances of a common subtype of type t. So, one have to use a heuristic
to find a type t' that works in most cases (I have changed the algorithm
between ocaml 1.00 and ocaml 1.01, that's why your example compiles with
the first version, but not with the second version).

Let us come back to your example. You can see here the type t' guessed:
#fun x -> (x :> runaround);;
- : (< null : bool; next : 'a; myself : 'a; .. > as 'a) -> runaround = <fun>
    \_________________________________________________/
                        t'
If you unify type t' with the type of self in class run2, you will get
type runaround. Indeed, method myself of self has type runaround, so
  runaround = 'a            (unification of types of myself)
            = type of self
The type runaround is closed, hence the error message.

The workaround is to use the more general coercion operator ( e : t' :> t )
when the other one fails.
  #class virtual run2 () as self =
  #    inherit runaround ()
  #    method myself = (self : #runaround :> runaround)
  #end;;
  class virtual run2 (unit) =
    method myself : runaround
    virtual null : bool
    virtual next : runaround
  end

    Jerome





      reply	other threads:[~1996-09-13 16:44 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-09-12 19:22 Andrew Conway
1996-09-13 12:31 ` Jerome Vouillon [this message]

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=Pine.3.89.9609131323.A21177-0100000@vedette \
    --to=vouillon@clipper.ens.fr \
    --cc=arc@sequence.stanford.edu \
    --cc=caml-list@pauillac.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