Take the 2-minute tour ×

I found something which I don't really understand while working on an ocaml project.

Suppose I'm using both the Array and List modules of OCaml standard library. They both implement the function length but have different types. In the List module, this is its type:

length: a' list -> int

And in the Array module, it has the type:

length: a' array -> int

But then I wanted you use both modules in the same module I was implementing, via the open keyword:

open List
open Array

When I tried to use the length function on a list, I had a type error during compilation.
Since OCaml is a strong statically typed language, I'm wondering why the compiler didn't know I wanted the length function of the list module since I declared I was using both.

share|improve this question
“I'm wondering why the compiler didn't know I wanted the length function of the list module since I declared I was using both” Yes, but what would be the type of fun s -> length s in this context then? –  Pascal Cuoq Jul 31 '12 at 9:33
Based on the answer of jrouquie, it'll be a' array -> int –  Joseph Elcid Jul 31 '12 at 9:52
Exactly, but in the case of an hypothetical OCaml compiler that tried to guess, there would be no single most general type in the OCaml type system for the function. You may be interested by Haskell's solution to this annoyance, type classes: haskell.org/tutorial/classes.html –  Pascal Cuoq Jul 31 '12 at 11:32
@PascalCuoq: you can do what Haskell does in OCaml with functors and module types, except that you will have to specify the instance of type class manually instead of being inferred –  newacct Jul 31 '12 at 19:32

1 Answer 1

up vote 7 down vote accepted

OCaml does not choose one function or another based on their types.

When you write

open Array

the functions of module Array are masking the ones of module List with the same name. When you later call the function length, OCaml looks for a function named length, finds Array.length, and complains that this function does not have a compatible type.

The usual way is to call List.length (instead of just length) if that is the function you need.

More generaly, OCaml does not have name overloading (i.e. having two functions or operators with the same name but distinct argument types), in particular because this would make type inference much harder.

share|improve this answer
Also: Try to avoid opening modules -- it makes reading your code much harder because a reader must remember which modules are open (and in which order); also as a reader you don't realize immediately where a function came from (from which open-ed module that is). –  lambdapower Jul 31 '12 at 14:42
I would like to add that opening modules is fine if the origin of the values/functions used remains clear. So don't open modules like List or Array which provide functions with generic names, but "open Printf" is perfectly fine because it provides functions printf, fprintf, etc. whose origin is obvious. –  Martin Jambon Jul 31 '12 at 17:15

Your Answer


