English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

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

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-01-10 (09:55)
From: Alex Baretta <alex@b...>
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)

The FreerP Project