caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp>
To: brian-l-smith@uiowa.edu
Cc: caml-list@inria.fr
Subject: OCaml's OO design Re: [Caml-list] Protected methods
Date: Mon, 22 Jul 2002 12:37:32 +0900	[thread overview]
Message-ID: <20020722123732Z.garrigue@kurims.kyoto-u.ac.jp> (raw)
In-Reply-To: <3D39ED57.8010403@uiowa.edu>

From: Brian Smith <brian-l-smith@uiowa.edu>
> Dmitry Bely wrote:
>  > Do not private Ocaml methods have in fact "protected" C++ semantics?
>  > They cannot be called directly but can be used in methods of inherited
>  > classes...
> 
> This was my impression as well. I think it would be a good idea to add
> something to the FAQ about how UML-ish/Java-ish
> private/protected/public/package visibility maps to O'Caml. And, if
> there isn't a direct mapping in some cases, then perhaps explanations of
> "workarounds" like the ones presented in this thread.

That would be certainly useful.
Unfortunately, my understanding of these issues is only partial, as
for most developpers of ocaml: they are much better at type theory of
O.O. than the quirks of each O.O. language.

OK, let's try:
Java                            OCaml
private                         private + hidden by interface
protected/subclasses only       private
default=package protected       public + type abstraction
protected                       public + type abstraction + export class
public                          public

Hidden by interface means defining a class type with some private
methods omitted.
Type abstraction means the workaround I presented earlier in this
thread, combining subtyping to hide methods, and use of an abstract
type to access hidden methods. You can choose to export or not the
class, to allow subclassing outside of the module.
However, if you want to enforce package protection for individual
methods while exporting the class, you have to abstract the type of
the method itself (Gerd Stolpmann's trick is not enough to avoid
redefinition):
        type hidden = ...
        method m : m_hidden

Why is ocaml's typing so different from other typed OO languages?
Essentially, this is because it chose a radically different
theoretical fundation, with structural rather than by-name typing.
This means that unrelated classes may have the same class type!
I believe this choice comes from another, more primitive choice, of
building object-polymoprhism on parametric polymorphism rather than
subtyping.

This provides for a very powerful language (at least in theory), where
you can type safely more OO programs and idioms than in any other
"usable" language.  But this also means some rough edges, as the basis
is rather experimental.

By the way, my experience with ocaml makes me think that the current
design is at least wrong on one point: mixing classes and class types.
As written above, two unrelated classes may have the same class type,
so there is no point in creating a class type for each class (it is
not "owned" by the class).
And a bad consequence of this choice is all these "unbound type
variable" errors, which are just due to the need to define a class
type.
By separating the two, and making classes first-class, one could clean
up some things, and for instance allow defining a class anywhere in an
expression (you would want to do a bit of lambda-lifting for
efficiency).

On the other hand, I also understand why it was designed that way:
abbreviating types is essential to get understandable error messages,
and this would have to be done explicitely, which some might find
verbose.
Yet, I wonder whether the separation would be a worthwhile (extensive)
change.

Cheers,

Jacques Garrigue
-------------------
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


  reply	other threads:[~2002-07-22  3:37 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-07-18 10:42 Alessandro Baretta
2002-07-18 11:01 ` Gerd Stolpmann
2002-07-18 11:44   ` Alessandro Baretta
2002-07-19  8:50     ` Jacques Garrigue
2002-07-19 10:01       ` Alessandro Baretta
2000-07-20  0:46         ` Jacques Garrigue
2002-07-20  7:41           ` Alessandro Baretta
2002-07-20  1:31         ` Jacques Garrigue
2002-07-20  7:48           ` Alessandro Baretta
2002-07-20 22:48 ` Dmitry Bely
2002-07-20 23:08   ` Brian Smith
2002-07-22  3:37     ` Jacques Garrigue [this message]
2002-07-22  4:20       ` OCaml's OO design " John Prevost
2002-07-20 23:54   ` Alessandro Baretta
2002-07-21  7:52     ` Dmitry Bely
2002-07-21 13:14       ` Alessandro Baretta

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=20020722123732Z.garrigue@kurims.kyoto-u.ac.jp \
    --to=garrigue@kurims.kyoto-u.ac.jp \
    --cc=brian-l-smith@uiowa.edu \
    --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).