One of the many interesting features in Caml is strong typing that might be explicit or implicit, i.e., type declaration is optional (it is always inferred).
One (dumb) example, list reversal, which already exists in the core libs (and we will use it):
let invert lst = List.rev lst ;;
Which Caml infers it is of type
val invert : 'a list -> 'a list = <fun>
(i.e., a function which takes a list and returns a list of the same type).
Let us now imagine that we want our own invert to support only lists of integers, we do:
let invert (lst : int list) = List.rev lst ;;
Caml infers:
val invert : int list -> int list = <fun>
One can be “lazy” when coding (i.e., not explicitly declaring types) a la scripting languages, but when going through the debugging hell that sometimes can happen with weak typing (in the Caml case it is really inference of a more general type than the correct one, or because of a wrong call, a wrong inference), explicitly type and force the offending code to show up.
Of course, one should try to be consistent and maintain the same style over all code, but I sometimes find my code to use both implicit and explicit conventions. This is due to laziness coupled to the fact that probably nobody will read me Caml code
.
Please share your thoughts
Filed in: Caml, declarative programming










