caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Peng Zang <peng.zang@gmail.com>
To: Michael Wohlwend <micha-1@fantasymail.de>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] oo type question
Date: Thu, 6 Mar 2008 11:58:12 -0500	[thread overview]
Message-ID: <200803061158.14882.peng.zang@gmail.com> (raw)
In-Reply-To: <200803061645.30224.micha-1@fantasymail.de>

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Thursday 06 March 2008 10:45:30 am Michael Wohlwend wrote:
> Am Donnerstag, 6. März 2008 14:18:29 schrieb Peng Zang:
> > let s = new store ;;
> > s#add (o1 :> <id:int>);;
> > s#add (o2 :> <id:int>);;
> >
> > Note how I coerce o1 and o2 both to a subtype before adding.  Now the
> > problem goes away because to the s#add method o1 and o2 have the same
> > type.
>
> yes, that's the easy way, but then I have to do casts all over the place,
> not very nice
>
> > 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.

Hmmm... I do not know of another easy way to do this.  Polymorphic methods 
cannot be inferred in general because IIRC it is undecidable?  That said, 
this special case seems simple enough.

Other (possibly lame) suggestions:

1) You can give some nice type alias to "<id:int; ..> as 'a" so the type 
specification looks simpler

2) You can go with the polymorphic class case and write a small helper 
function, eg:

  let addwithcoerce s a = s#add (a :> <id:int>)

to hide the coercions.

Perhaps others on this list will have additional ideas too.

Regards,
Peng


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.7 (GNU/Linux)

iD8DBQFH0CKmfIRcEFL/JewRAkLXAKCLog1BC8wViOJ+yF/TjoG4T+5KDACgxXJ1
HiUc6i+D9bvpKWn/zCbWEf0=
=R6fR
-----END PGP SIGNATURE-----


  reply	other threads:[~2008-03-06 16:58 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 [this message]
2008-03-10  8:20     ` Jacques Garrigue
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=200803061158.14882.peng.zang@gmail.com \
    --to=peng.zang@gmail.com \
    --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).