From: brogoff@speakeasy.net
To: Benjamin Geer <ben@socialtools.net>
Cc: Brian Hurt <bhurt@spnz.org>, "caml-list@inria.fr" <caml-list@inria.fr>
Subject: Re: [Caml-list] does class polymorphism need to be so complicated?
Date: Wed, 20 Aug 2003 13:39:20 -0700 (PDT) [thread overview]
Message-ID: <Pine.LNX.4.44.0308201312120.7047-100000@grace.speakeasy.net> (raw)
In-Reply-To: <3F43BBB8.9090806@socialtools.net>
On Wed, 20 Aug 2003, Benjamin Geer wrote:
> Unless I've missed something, this only works if none of the classes of
> type fbbq have any additional methods. Here's an example that doesn't work:
Yes, OCaml doesn't have implicit subtyping. The language manual is quite clear
on that. If you want to solve your problem, you'll need to coerce, and you
can solve it fairly simply by replacing the troublesome line below with one of
the following
tp#process (et :> fbbq) ;;
or
let proc o = (new thing_processor)#process (o :> fbbq);;
proc et;;
or stuff like that. You get the picture. The (row) type representing
"fbbq or anything which has the same methods" is this
type 'a fbbq_c = 'a constraint 'a =
< foo: int->int;
bar: unit->string;
baz: unit->bool;
quux: unit->unit; .. >
where as you can see there is a type variable to handle that row polymorphism.
So, if you want a method which takes that as an argument, it has to be
polymorphic. Easier to solve it as I did above with a function wrappers and
coercions. A solution using polymorphic methods is sketched next, where I
use them to fake implicit subtyping
class type thing =
object
method foo : int -> int
method bar : string
method baz : bool
method quux : unit
end;;
class thing_processor =
object
method process : 'a . (#thing as 'a ) -> unit =
fun obj ->
begin
obj#quux;
print_string (string_of_int (obj#foo 1));
print_string obj#bar;
print_string (string_of_bool obj#baz);
print_endline ""
end
end ;;
let tp = new thing_processor ;;
tp#process et ;;
so I guess you are now left with a Perl like TMTOWTDI situation.
Overall, yes, the class system is complicated.
-- Brian
> type fbbq =
> <foo: int->int; bar: string; baz: bool; quux: unit > ;;
>
> class thing x_init =
> object (self)
> val mutable x = x_init
> method foo y = x + y
> method bar = string_of_int (self#foo 2)
> method baz = (x = 1)
> method quux = print_string (string_of_int x)
> end;;
>
> class extra_thing x_init =
> object (self)
> inherit thing x_init
> method quuux = self#quux; self#quux
> end;;
>
> class thing_processor =
> object
> method process (obj : fbbq) =
> obj#quux;
> print_string (string_of_int (obj#foo 1));
> print_string obj#bar;
> print_string (string_of_bool obj#baz)
> end ;;
>
> let et = new extra_thing 1 ;;
> let tp = new thing_processor ;;
> tp#process et ;;
>
> The call to 'process' produces the following error:
>
> This expression has type
> extra_thing =
> < bar : string; baz : bool; foo : int -> int; quuux : unit; quux :
> unit >
> but is here used with type
> fbbq = < bar : string; baz : bool; foo : int -> int; quux : unit >
> Only the first object type has a method quuux
>
> So this approach doesn't fit the requirement, which is to be able to use
> a derived class anywhere its base class can be used.
>
> Ben
>
> -------------------
> 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
>
-------------------
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
next prev parent reply other threads:[~2003-08-20 20:39 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 [this message]
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
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=Pine.LNX.4.44.0308201312120.7047-100000@grace.speakeasy.net \
--to=brogoff@speakeasy.net \
--cc=ben@socialtools.net \
--cc=bhurt@spnz.org \
--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