From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p7RBOGxn029690 for ; Sat, 27 Aug 2011 13:24:16 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ArsCAITSWE7RVdIpimdsb2JhbABCnHKLLwgUAQEBCgkNBxIGIYFAAQEBAQIBEgImBgE4AQMBCwEFBQ4mEjQBBQEcBjWHUAScFQqPEIRbiSgCAwaFZmAEh2KLPYY2his8gUaCMA X-IronPort-AV: E=Sophos;i="4.68,289,1312149600"; d="scan'208";a="117382505" Received: from mail-pz0-f41.google.com ([209.85.210.41]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 27 Aug 2011 13:24:10 +0200 Received: by mail-pz0-f41.google.com with SMTP id 4so6875527pzk.14 for ; Sat, 27 Aug 2011 04:24:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=sender:subject:mime-version:content-type:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to:x-mailer; bh=GHW4vLBkqHMUnImTO1ycK+f17JGVMEvrNW7N07w4gCk=; b=PW5+jKyxppGJ1//2bXZzUSDir0BVhCiU1pyCHnNnbsFarhx0fDQ0gtd9gCs/D7eaTw sdvLpYCasmMk32sCbYknYkjWNS29A3hp9ezruDrIVJU8OoAGHf1I2RuOXo4YD4ET1m4h bKSwdYn+uWbCXdr7BI+cTWUXhbviyq3txFag8= Received: by 10.143.76.8 with SMTP id d8mr1199728wfl.400.1314444250845; Sat, 27 Aug 2011 04:24:10 -0700 (PDT) Received: from tet.garrigue.jp (58x158x128x157.ap58.ftth.ucom.ne.jp [58.158.128.157]) by mx.google.com with ESMTPS id w18sm479228wfg.21.2011.08.27.04.24.08 (version=TLSv1/SSLv3 cipher=OTHER); Sat, 27 Aug 2011 04:24:09 -0700 (PDT) Sender: Jacques Garrigue Mime-Version: 1.0 (Apple Message framework v1084) Content-Type: text/plain; charset=us-ascii From: Jacques Garrigue In-Reply-To: <4E58CCC3.4040901@gmail.com> Date: Sat, 27 Aug 2011 20:24:06 +0900 Cc: caml-list@inria.fr Content-Transfer-Encoding: 7bit Message-Id: References: <4E58CCC3.4040901@gmail.com> To: Chris Yocum X-Mailer: Apple Mail (2.1084) Subject: Re: [Caml-list] Ocaml and the Fragile Base Class Problem 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