caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: Chris Yocum <cyocum@gmail.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Ocaml and the Fragile Base Class Problem
Date: Sat, 27 Aug 2011 20:24:06 +0900	[thread overview]
Message-ID: <F268A478-681B-4357-98DF-0374E8417523@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <4E58CCC3.4040901@gmail.com>

On 2011/08/27, at 19:53, Chris Yocum wrote:

> A friend of mine is giving a talk about "monkey patching" entitled
> "Monkey patching, subclassing, and accidental overriding"
> (http://aaroncrane.co.uk/talks/monkey_patching_subclassing/paper.html).
> I was wondering how Ocaml deals with this situation or if it is even a
> problem at all in Ocaml?  I mocked up some code:
> 
> class base =
> object
>  method meth x =
>    print_endline "base";
>    print_endline (string_of_int x)
> end
> 
> class deriv =
> object
>  inherit base
>  method meth x =
>    print_endline "deriv";
>    print_endline (string_of_int x)
> end
> 
> which kind of(?) shows the problem in Ocaml.  He suggests in his paper
> that using a Meta-Object Protocol is the way around this.  What do you
> think?


You can require overriding to be explicit by activating warning 7:

$ ocaml -w +7
        OCaml version 3.13.0+dev6 (2011-07-29)

# class c = object method x = 1 end;;
class c : object method x : int end
# class d = object inherit c method x = 2 end;;
Warning 7: the method x is overridden.
class d : object method x : int end

Note also that in ocaml a method only has one signature,
so the problems with API changes doesn't apply here:
you get an error if the type changed in the base class,
and a warning if the method name was changed in the base class.

However, in this respect I think that a "final" keyword is missing in ocaml.
Namely, you have no way to say that you don't want subclasses to modify
a method (because you want to be able to redefine it yourself, for instance).
But OCaml is not an object-only language, so if you want this kind of
guarantee you can use functors: specify exactly what the user should provide,
without allowing a posteriori overriding.

Jacques Garrigue

  reply	other threads:[~2011-08-27 11:24 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-08-27 10:53 Chris Yocum
2011-08-27 11:24 ` Jacques Garrigue [this message]
2011-08-27 15:06 ` Gerd Stolpmann
2011-08-27 16:59   ` David Baelde
2011-08-27 19:37     ` Gerd Stolpmann
2011-08-27 20:21       ` Jeff Meister
2011-08-27 23:08         ` Gerd Stolpmann
2011-08-28  9:31           ` Andreas Rossberg
2011-08-28 10:04             ` Guillaume Yziquel
2011-08-28 10:11             ` Gerd Stolpmann
2011-08-28 10:50               ` Andreas Rossberg
2011-08-29  3:35           ` Jacques Garrigue
2011-08-29 11:19             ` Chris Yocum
2011-08-29 11:47               ` Guillaume Yziquel
2011-08-29 12:03                 ` Chris Yocum
2011-08-31 21:33             ` Alain Frisch
2011-08-31 23:39               ` Jacques Garrigue
2011-08-28 17:58       ` Julien Signoles

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=F268A478-681B-4357-98DF-0374E8417523@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=cyocum@gmail.com \
    /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).