caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: micha-1@fantasymail.de
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] oo type question
Date: Mon, 10 Mar 2008 17:20:43 +0900 (JST)	[thread overview]
Message-ID: <20080310.172043.180101905.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <200803061645.30224.micha-1@fantasymail.de>

From: Michael Wohlwend <micha-1@fantasymail.de>
> Am Donnerstag, 6. März 2008 14:18:29 schrieb Peng Zang:
> > BUT, what I think you really want is a polymorphic method, not a
> > polymorphic class.  The manual has a decent explanation under polymohrphic
> > methods in the Objects section.  So I think you want this:
> >   method add : 'a. <id:int; ..> as 'a -> unit =
> >     fun o -> ids <- o#id :: ids
> 
> I know that version and of course it works. I only  didn't understand (and 
> still do not) why it's not possible to just declare
> method add (a: #someClass) ... which seems natural for me. And it works for 
> normal functions that way.
> 
> If you have many classes (actually it's for adding widgets to group widgets) 
> it seems just a bit too complicated and not a natural way to declare such a 
> simple thing.

One reason it is not so simple for methods is that they belong to
objects. So if you write
   method add (a : #someClass) ...
which is really equivalent to
   method add (a : <some methods; .. > as 'a)
it is not clear where the variable should be bound. This could be
either at the class level (for the whole object, as in your first
example) or at the method level. So you need an explicit syntax to
tell which one it is to the type system.

Another reason, a bit more technical, is that polymorphic methods do
not behave like polymorhic functions. In particular, they cannot be
instantiated automatically when needed: i.e., you cannot pass an
object with a polymorphic method where a monomorphic method is
expected. Currently, even subtyping is not allowed between polymorphic
methods and their monomorphics instances (this may change in 3.11).
So this seems a bad idea to create a potentially incompatible
polymorphic method without some explicit direction from the
programmer. Note that the type annotation #someClass is not sufficient
direction, because it does not request any polymorphism: it is not an
error for the actual method to be of type "someClass -> ..." (I'm not
sure it is what most people expect, but it is the way ocaml is
defined)

A small remark yet: in interfaces (and class types) it is ok to write
  class c : object
    method add : #someClass -> ...
  end
Here it is automatically assumed that the type variable is bound at
the method level.

Jacques Garrigue


  parent reply	other threads:[~2008-03-10  8:21 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-03-06 12:52 Michael Wohlwend
2008-03-06 13:18 ` [Caml-list] " Peng Zang
2008-03-06 15:45   ` Michael Wohlwend
2008-03-06 16:58     ` Peng Zang
2008-03-10  8:20     ` Jacques Garrigue [this message]
2008-03-10  8:38       ` Michael Wohlwend

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=20080310.172043.180101905.garrigue@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=caml-list@yquem.inria.fr \
    --cc=micha-1@fantasymail.de \
    /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).