Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
"OCaml gives you only monomorphic methods in classes."
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-12-31 (14:03)
From: Kuba Ober <ober.14@o...>
Subject: Re: [Caml-list] "OCaml gives you only monomorphic methods in classes."
> >> 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++ 

Cheers, Kuba