[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: | 2007-04-24 (22:22) |
From: | Philippe Wang <lists@p...> |
Subject: | Re: [Caml-list] typing question |
micha wrote: > Hi, > > why is the type of register1 'a -> string but the type of register2 > _'a -> string? > > > cheers > Michael > > > > let symbol_id = ref 0;; > let register1 fkt = > let name = "symbol-" ^ (string_of_int !symbol_id) in > incr symbol_id; > Callback.register name fkt; > name > ;; > > let register2 = > let symbol_id = ref 1 in > fun fkt -> let name = "symbol-" ^ (string_of_int !symbol_id) in > incr symbol_id; > Callback.register name fkt; > name > ;; > It is because register2 is detected as an "expansive" expression. "Expansive expressions" are those that we can't generalize without taking the risk to allow type errors at execution, so its type variable remains unknown and can't be "forall 'a. 'a". Take this simple example : let x = ref [] So x : '_a ref, because if x were 'a ref, then you could write x := [343] ; x := ["hello"] and there would be a type error at runtime. Thus, even if an expression such as id2 in : let id x = x ;; let id2 = id id ;; does exactly the same computing as id, it can't be 'a -> 'a because id2 is an "application" and ocaml doesn't generalize applications... Good luck, -- Philippe Wang