Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Kuba Ober <ober.14@osu.edu>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] "OCaml gives you only monomorphic methods in classes."
Date: Mon, 31 Dec 2007 09:02:54 -0500	[thread overview]
Message-ID: <200712310902.54525.ober.14@osu.edu> (raw)
In-Reply-To: <1A5E39DC-CCAE-48B2-AC06-C671143A6C50@mac.com>

> >> Jon, consider the case of Java or C++ method (and function)
> >> overloading. This form of static dispatch is quite distinct from
> >> virtual method dispatch, and is indeed at odds with type inference.
> >
> > I see. Still, the solution seems easy enough. Just treat the
> > overloaded types
> > as part of the function name:
> >  foo(int n)
> > becomes:
> >  foo_int n
> > When you come across:
> >  foo(a)
> > you look up the statically inferred type of "a" and just bail if it
> > fails to
> > match any of the overloads.

> Oh, were you proposing a language feature? In that case, consider:
> let bar x = foo x
> How to compile that?

Check for all foo_xxx, and expand to
let bar_int x = foo_int x
let bar_float x = foo_float x
and so on.

The only thing I'm worried about is the fact that for one type,
there doesn't necessarily need to be a foo_xxx function. This naming solution 
isn't overly clean, so maybe it'd be better to do it slightly differently.

What about having the function name implicitly reference the signature, and do 
pattern matching on that?

So let's say we'd have

let add x y = x + y
let add x y = x + int_of_float y
let add x y = x +. y

let add2 x y = (x + 1) + (y - 1)
let add2 x y = (x + 1) + (int_of_float y - 1)
let add2 x y = (x +. 1.) + (y -. 1.)

And now we do

let add (x : int) y = add2 x y

This would replace the first two add functions, but not the third.

Presumably, for this feature to be safe to add to the language, there should
be some syntactic sugar to conveniently enable it for a function. No clue how
that'd look.

This would be a very useful addition to the language, although I'm sure it 
could make compilation longer in some cases, as the type inferrer would have
more choices to work with.

The "good old" mangling of the signature into the symbol is something we'd 
better not have to do manually.

For now I'm working on a very basic C++-to-Ocaml "converter" (in the spirit of 
web2c, i.e. only to work on one codebase), and this is definitely one of the 
issues I'm facing, as the code pretty much uses the whole spectrum of C++ 
features.

Cheers, Kuba


  parent reply	other threads:[~2007-12-31 14:03 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-12-28 23:37 Jon Harrop
2007-12-28 23:55 ` [Caml-list] " Gordon Henriksen
2007-12-29  0:27   ` Jon Harrop
2007-12-29  1:19     ` Gordon Henriksen
2007-12-29  1:30       ` Jon Harrop
2007-12-31 14:02       ` Kuba Ober [this message]
2007-12-29  4:31 ` Dylan William Hardison
2007-12-29  7:09   ` Jon Harrop
2007-12-29  6:30 ` brogoff
2007-12-29  7:11   ` Jon Harrop
2007-12-29 16:57     ` brogoff
2008-01-08  2:30     ` Jacques Garrigue
2008-01-08  9:42       ` Jon Harrop
2008-01-08 13:45         ` Peng Zang
2008-01-08 17:29         ` brogoff
2008-01-08 21:25         ` Paolo Donadeo

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=200712310902.54525.ober.14@osu.edu \
    --to=ober.14@osu.edu \
    --cc=caml-list@yquem.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