Browse thread
[Caml-list] failwith, raise and type inference
-
Paul Guyot
- Frederic van der Plancke
- Correnson_Loïc
- Nicolas Cannasse
- Luc Maranget
[
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: | Luc Maranget <Luc.Maranget@i...> |
| Subject: | Re: [Caml-list] failwith, raise and type inference |
> Hello,
>
> My students came up with a behavior of ocaml I couldn't explain.
> I have exactly the same result with 3.07pl2 and a fresh copy checked
> out from CVS.
>
> >let rec f1 key dict =
> > match dict with
> > [] -> failwith "The key " ^ key ^ " could not be found"
> > | (aKey, aValue)::tail ->
> > if (aKey = key)
> > then aValue
> > else f1 key tail;;
>
> is of type:
>
> val f1 : string -> (string * string) list -> string = <fun>
>
> Any idea?
I think I have a clue :
failwith "The key " ^ key ^ " could not be found"
is parsed as
(failwith "The key ") ^ key ^ " could not be found"
Hence the overall type of this expression is string.
The other result of the pattern matching matching has the same type
as aValue
Finally, you get that the type of aValue (second component of
your pairs is string).
I think you can check my interpretation by issuing
>f1 "bonga" []
>;;
Exception: Failure "The key ".
Of course, the intended coding is
failwith ("The key " ^ key ^ " could not be found")
Admitedly caml syntax is not very beginner friendly.
But the observed parsing has some internal logics.
On usualy understand that f x + y is in fact (f x) + y
But here, with failwith being a ``special'' function and ^ a ``special''
operator, well...
Cheers,
--Luc Maranget
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners