Version française
Home     About     Download     Resources     Contact us    
Browse thread
Feature request : Tuples vs. records
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Till Varoquaux <till.varoquaux@g...>
Subject: Re: [Caml-list] Feature request : Tuples vs. records
On 2/22/07, David Teller <David.Teller@ens-lyon.org> wrote:
> Sounds interesting. Do you have documentation on this use of records for
> general recursivity ?
>
> Thanks,
>  David

I really put my foot in my mouth for my previous post (I had just read
the part about unlabeled tupple in SML...). Anyway here I come:

let rec f a b=
 if false then
  (*Although this is never called it breaks polymorphisme*)
  f print_string "a";
 a b;;

And here is the same using records (and having a more general type):

type my_rec={f:'a 'b.('a -> 'b) -> 'a -> 'b};;

let rec r={
 f=(fun a b ->
     if false then
      (*Polymorphisme is not b0rken...*)
      r.f print_string "a";
     a b
 )
};;
r.f print_int 5;;

This is a very useless example. Thierry Martinez (I know you are
reading us) had a cool example and some of the fixpoint operators on:
http://okmij.org/ftp/ML/fixpoints.ml
use this trick.

Cheers,
Till




>
> Till Varoquaux a écrit :
> > Another simple difference is that you have to declare the type of the
> > records whereas types of tupples can be infered. Since the typechecer
> > actually uses the type information given, you can use polymorphic
> > fields to implement general reccursivity.
> > I would also mention row polymorphisme and "mutable" as notable
> > differences. You could also note that they are not always
> > interchangeable: whilst you wouldn't want to define a new record type
> > for every tupple you use (very verbose), direct access to a defined
> > field and the "with" keyword (e.g let b={a with x=1}) make records
> > nice to handle large structures.
> >
> > Till
>
>