caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Chris Clearwater <chris@sharkzone.com>
To: caml-list@inria.fr
Subject: Re: [Caml-list] does class polymorphism need to be so complicated?
Date: Thu, 21 Aug 2003 13:44:33 -0500	[thread overview]
Message-ID: <20030821184433.GA3068@pyramid.twistedmatrix.com> (raw)
In-Reply-To: <20030821102946Y.garrigue@kurims.kyoto-u.ac.jp>

On Thu, Aug 21, 2003 at 10:29:46AM +0900, Jacques Garrigue wrote:
> Another approach which was not described yet, and which I use in
> lablgtk for instance, is to add a coercion method to classes which form
> the top of a hierarchy. This way you just have to write
>     printer#print obj#printable
> in place of a coercion, which may be shorter and avoid strange error
> messages when failing.
> To do this you just have to add the following to the printable virtual
> class:
>   class virtual printable = object (self)
>     method virtual ...
>     method printable = (self :> printable)
>   end

Another approach if you are using abstract base classes to simulate an
interface it to ditch the object system altogether and use closures.
You might do something like this:

printable.ml:

type printable = {
    print: unit -> unit;
}

let create p print = {
    print = fun () -> print p
}

let print p = p.print ()

circle.ml:

type circle = {radius: float}

let create r = {radius=r}
let print c = Printf.printf "Circle, radius: %f" c.radius
let as_printable c = Printable.create c print

main.ml:

let c = Circle.create 10.0
let p = Circle.as_printable c
let _ = Printable.print p

Some nice side effects of this is that you get better type inference,
non-virtual function calls when you dont need an abstract type
(Circle.print) and probally better performance for virtual calls as well.
Also keep in mind that for any virtual function that takes more
arguments besides the object itself (most of them) you can avoid the
unit hack and use partial application (say draw took a size and color
arguments):

drawable.ml:
type drawable = { draw: int -> color -> unit }
let create d draw = { draw = draw d }
let draw d = d.draw

 etc..
 main.ml somewhere:
 .. Drawable.draw c 10 red

> And if you're going to change the requirements of a method in the
> middle of your development, this means that there was something wrong
> in your design. Overall object-oriented languages are much weaker
> than functional languages at changing design afterwards.

With this method you just change the as_printable functions :)

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  parent reply	other threads:[~2003-08-21 18:45 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-08-20 15:42 Benjamin Geer
2003-08-20 16:05 ` Brian Hurt
2003-08-20 16:19   ` Richard Jones
2003-08-20 16:25   ` Benjamin Geer
2003-08-20 17:09     ` brogoff
2003-08-20 17:25       ` Jacques Carette
2003-08-20 23:34         ` Jacques Garrigue
2003-08-21 13:27           ` Jacques Carette
2003-08-20 18:19       ` Benjamin Geer
2003-08-20 20:39         ` brogoff
2003-08-20 21:04           ` Benjamin Geer
2003-08-21  0:28             ` Jacques Garrigue
2003-08-21  8:17               ` Benjamin Geer
2003-08-21  8:58                 ` Jacques Garrigue
2003-08-21  9:38                   ` Benjamin Geer
2003-08-21 11:44                     ` Remi Vanicat
2003-08-21 13:11                       ` Richard Jones
2003-08-21 16:41                         ` Remi Vanicat
2003-08-21 18:04                     ` brogoff
2003-08-21 20:20                       ` Benjamin Geer
2003-08-21 23:35                         ` Benjamin Geer
2003-08-22  3:59                           ` Jacques Garrigue
2003-08-22  7:12                             ` Benjamin Geer
2003-08-21 13:38                   ` Benjamin Geer
2003-08-21  0:58             ` brogoff
2003-08-20 23:40           ` Benjamin Geer
2003-08-21  1:29             ` Jacques Garrigue
2003-08-21  9:19               ` Benjamin Geer
2003-08-21 18:44               ` Chris Clearwater [this message]
2003-08-20 20:43   ` Issac Trotts

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=20030821184433.GA3068@pyramid.twistedmatrix.com \
    --to=chris@sharkzone.com \
    --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).