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
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-03-04 (04:57)
From: Brian Hurt <bhurt@s...>
Subject: Re: [Caml-list] stl?

On Tue, 3 Mar 2009, Yoann Padioleau wrote:

> Brian Hurt <bhurt@spnz.org> writes:
>> On Tue, 3 Mar 2009, Jon Harrop wrote:
>>> Functors give
>>> you the same capability in OCaml but they are rarely used precisely because
>>> the functionality is not very useful.
>> I think I disagree with this.  I think functors aren't used very much
>> in Ocaml because:
>  0) they are intrusive! putting code inside a functor may entail the
> need to modify also lots of related code. That's one of the worst
> thing for a programming feature. Your modification can not be local. I
> hate monad for the same reason, and I like ocaml exception mechanism,
> and using sometimes global refs for the same reason.


Here's what you can do.  Say you had an old function:
 	let foo x = ...
and you want to functorize it for whatever reason.  But you have lots of 
code depending upon the old type signature that you don't want to change. 
First, you functorize foo:

 	module Make(X: Whatever) = struct let foo x = ... end;;

Then you include the "classic foo", like:

 	module T = Make(struct ... old defns ... end);;
 	include T;;

Where ... old defns ... is all the original types and functions that foo 
used that are now provided by the functor.  So you now have a module that 
exports both a "functorized" foo and a "classic" foo, and all the old code 
continues to just work (after a recompile, natch).

This is much the same trick you use in Haskell when adding a typeclass 
dependency to the signature of a function- so long as you provide a 
typeclass instance for the old type, the old code continues to work with a 

I also comment, this is also a problem C++ templates have- templatizing a 
class in C++ is also an "intrusive" change.