Browse thread
[Caml-list] does class polymorphism need to be so complicated?
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Benjamin Geer <ben@s...> |
| Subject: | Re: [Caml-list] does class polymorphism need to be so complicated? |
Jacques Garrigue wrote:
> Here there may be a deeper misunderstanding about the ocaml type
> system: if a subclass does not add methods to its superclass, its type
> does not change.
> That is, I would expect all connections to have the same type, and as
> a result there is no need for considering the more general
> #connection.
I think this would place an undesirable restriction on driver authors.
They may want to add additional methods to their implementation of
#connection, for use by other classes in the driver, even if the user
will never be able to call those methods.
In general, this approach allows for a complete separation between
interface and implementation: the implementing class can always have
more methods than the interface, if this makes the implementation more
convenient to write.
Alternatively, you could use a virtual base class 'connection', and
always downcast the implementing class before passing it to application
code. But this places an additional burden on the library author.
> OK, there's also another way to do it, without inheritance. I just
> tried not to be confusing.
>
> class type printer = object
> method virtual print : #printable -> unit
> end
>
> class my_printer () = object (self : #printer)
> method print obj = ...
> end
>
> Looks a bit strange at first, but it does the work.
Is there a way to write a class that implements more than one interface?
I've tried the following, but it produces a syntax error:
class type virtual printer = object
method virtual print : #printable -> unit
end ;;
class type virtual talker = object
method virtual talk : #printable -> unit
end ;;
class my_printer_talker () = object (self : #printer; #talker)
method print obj = (* ... *)
method talk obj = (* ... *)
end ;;
Am I right in guessing that you have to use multiple inheritance to
achieve this?
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