Version française
Home     About     Download     Resources     Contact us    
Browse thread
mutable and polymorphism
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Andreas Rossberg <rossberg@m...>
Subject: Re: [Caml-list] mutable and polymorphism
On Sep 15, 2010, at 19:10, Radu Grigore wrote:
> Compile the following three files one-by-one.
>  (*a.ml*) let _ = ref () in let f = fun _ -> () in f 1; f 'a'
>  (*b.ml*) let f = let _ = () in fun _ -> () in f 1; f 'a'
>  (*c.ml*) let f = let _ = ref () in fun _ -> () in f 1; f 'a'
> The files a.ml and b.ml compile; the file c.ml fails with
>  Error: This expression has type char
>    but an expression was expected of type int
> Could someone explain why having a mutable field ("contents" in this
> case) restricts the polymorphism of f?

Have a look at this variant of c.ml and its "potential" if it type- 
checked:

>  let f = let xs = ref [] in fun x -> xs := x :: !xs in f 1; f 'a'


/Andreas