Some information about the need for type annotations in OCaml tutorial.

This commit is contained in:
Daniil Baturin 2014-09-13 00:46:46 +07:00
parent 166fb997a0
commit 807a958c78

View File

@ -82,7 +82,13 @@ let foo' = foo * 2 ;;
(* Since OCaml compiler infers types automatically, you normally don't need to (* Since OCaml compiler infers types automatically, you normally don't need to
specify argument types explicitly. However, you can do it if specify argument types explicitly. However, you can do it if
you want or need to. *) you want or need to. *)
let inc_int (x: int) = x + 1 ;; let inc_int (x: int) : int = x + 1 ;;
(* One of the cases when explicit type annotations may be needed is
resolving ambiguity between two record types that have fields with
the same name. The alternative is to encapsulate those types in
modules, but both topics are a bit out of scope of this
tutorial. *)
(* You need to mark recursive function definitions as such with "rec" keyword. *) (* You need to mark recursive function definitions as such with "rec" keyword. *)
let rec factorial n = let rec factorial n =