caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: remy <Didier.Remy@inria.fr>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Question about objects and method overriding
Date: Wed, 05 Feb 2014 19:22:17 +0100	[thread overview]
Message-ID: <52F28159.3050507@inria.fr> (raw)
In-Reply-To: <CABooLwN_1vpyQAfA53cAiTk_5zACny1YZBt+dLd7h6WYyQiXQw@mail.gmail.com>

Tom,

I am afraid that what you would like to do is unsafe. Consider the following
example:

     class a  = object method a = 1 end
     class ac = object (self : 'a) method a = 1 method c = 3 end
     class ab = object method a = 1 method b = new a end

     class abc = object (self : 'a)
       method a  = (self # b) # c
       method b = self
       method c = 2
     end

     let bug =
       let abc = new abc in
       let ab = (abc :> ab) in
       let ab' : ab = (ab with b = new a)
       ab' # a

This is typable in OCaml if you replace the penultimate line by let ab' = ab
According to your proposal, the penultimate line should typecheck as well
because we change method b of type a by a method of the same type.

However, the evaluation of bug creates a new object ab' with methods a, b,
and c but where b returns an object a' with a single method a.  Therefore,
the evaluation of ab' # a will reduce to ab' # b # c then to a' # c which
will break.

----------------

The problem is that methods may receive self as argument, but this is not
traced in the object types---only in the class types.

If you replace method override by field override with a val call_b
and a method set_b as suggested, typechecking will break because
the type of self will then appear contra-variantly in method set_b which
will prevent subtyping (abc : ab).

In order to typecheck method overriding safely, one has to prevent the use
of subtyping altogether or track it field by field, but this quickly becomes
very tricky.

See for instance "From classes to objects via subtyping"
http://cristal.inria.fr/~remy/publications_abstracts.html#Remy/classes-to-objects/esop

         Didier

      parent reply	other threads:[~2014-02-05 18:22 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-02 11:55 Tom Ridge
2014-02-02 12:43 ` Jacques Garrigue
2014-02-02 12:50   ` Tom Ridge
2014-02-03 10:35 ` Goswin von Brederlow
2014-02-03 12:24   ` Tom Ridge
2014-02-03 15:54     ` Goswin von Brederlow
2014-02-03 13:55   ` Alain Frisch
2014-02-03 15:12     ` Tom Ridge
2014-02-05 18:22 ` remy [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=52F28159.3050507@inria.fr \
    --to=didier.remy@inria.fr \
    --cc=caml-list@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;
as well as URLs for NNTP newsgroup(s).