Am Samstag, den 27.08.2011, 11:53 +0100 schrieb Chris Yocum:
> Hello,
> 
> 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?

I think it is a non-issue in practice. Simply because inheritance is
quite seldom in Ocaml, even in programs using a lot of OO features.
There is an attractive alternative, namely passing functions down to
change the behavior of objects. So, you would normally define

class base ?meth () =
object
  method meth x =
    match meth with
      | None ->
          print_endline "base";
          print_endline (string_of_int x)
      | Some f ->
          f x
end

class deriv = 
  base ~meth:(fun x ->
                print_endline "deriv";
                print_endline (string_of_int x)
             )
       ()

You'd do something like this for all mutable behavior (maybe you
wouldn't use individual functions for overriding, but some more
elaborate data structure). Of course, this is generally a less powerful
technique, but you often don't need the full power of inheritance. This
is attractive for getting more control, and looks less like patching the
algorithms of the base class.

Also, you don't need inheritance in Ocaml for declaring subtyping
relations. So, it is entirely possible to avoid the "inherit" keyword,
and have nevertheless a lot of object-oriented design.

Gerd

> 
> Thanks,
> Chris Yocum
> 

-- 
------------------------------------------------------------
Gerd Stolpmann, Darmstadt, Germany    [email protected]
Creator of GODI and camlcity.org.
Contact details:        http://www.camlcity.org/contact.html
Company homepage:       http://www.gerd-stolpmann.de
*** Searching for new projects! Need consulting for system
*** programming in Ocaml? Gerd Stolpmann can help you.
------------------------------------------------------------


-- 
Caml-list mailing list.  Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

Reply via email to