Browse thread
Why does value restriction not apply to the empty list ?
[
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: | -- (:) |
| From: | Richard Jones <rich@a...> |
| Subject: | Re: [Caml-list] Why does value restriction not apply to the empty list ? |
On Sat, Jan 10, 2009 at 12:34:22PM +0100, Antoine Delignat-Lavaud wrote:
> In Ocaml, the program
> let el = [] in if List.length el > 0 then (List.hd el)+(int_of_string
> (List.hd el)) else 0 ;;
> yields not type error and returns 0 despite the use of el as both an int
> list and a string list.
>
> Thus, I am wondering why does value restriction not apply to the empty
> list in Ocaml. I don't think it's possible to do a cast with the empty
> list (it is empty after all) but I don't see any benefit in doing so.
It's a strange one ... when the if statement appears as a toplevel
statement, OCaml infers the type 'a list for the list:
# let el = [] ;;
val el : 'a list = []
# if List.length el > 0 then (List.hd el)+(int_of_string (List.hd el)) else 0;;
- : int = 0
# el ;;
- : 'a list = []
But the same if statement within a function definition causes an error:
# let f el =
if List.length el > 0 then (List.hd el)+(int_of_string (List.hd el)) else 0;;
^^^^^^^^^^
This expression has type int but is here used with type string
Rich.
--
Richard Jones
Red Hat