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

generic functions
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: -- (:) From: Alex Baretta Subject: Re: [Caml-list] generic functions
```Brian Hurt wrote:
> On Sun, 9 Jan 2005 wiedergaenger@fastmail.fm wrote:
>
>>let foo (x : int) = x*x;;
>>let foo (x : float) = x*.x;;
>>

> With the exception of certain artificial contests (Paul Graham) I've never
> met a real world problem that needed overloading, or even benefitted
> signifigantly from overloading that didn't benefit just as much or more
> from one of the solutions above.
>
> Brian

As a member of the International Caml's Jihad, I'd like to spend two
cents of dynamite on this topic: type inference is as good as honey in
large scale industrial projects, where sometimes the typing of complex
recursive polymorphic algorithms is not evident until you get the
printout from the compiler or the toplevel. Thank you, O great
Unification Algorithm.

But, then again, extensional polymorphism is also a much needed feature.
In my company we depend heavily on on extensional polymorphism to create
exstensibile functions. We have implemented this on top of explicitly
polymorphic types: i.e. polymorphic variant types.

Let us all faithfully await the Coming of the the Gcaml... And meanwhile
let us use polymorphic variants to get the same effect.

type poly_int = [ `Int of int ]
type poly_float = [ `Float of float ]

let foo_int foo = function
| `Int x -> `Int(x * x)
| other -> foo other

let foo_float foo = function
| `Float x -> `Float(x *. x)
| other -> foo other

let rec foo x = match x with
| #poly_int -> foo_int foo x
| #poly_float -> foo_float foo x

All hail Ocaml!

Alex, after too many nights spent writing typechecking code...

--
*********************************************************************
Baretta DE&IT
A division of Baretta SRL

tel. +39 02 370 111 55
fax. +39 02 370 111 54

Our technology:

The Application System/Xcaml (AS/Xcaml)
<http://www.asxcaml.org/>

The FreerP Project
<http://www.freerp.org/>

```