Ocaml modular programming

I found something that I really don't understand when working on a project ocaml.

Suppose I use modules Arrayand the Liststandard OCaml library. They implement a function length, but have different types. In a module, Listthis is its type:

length: a' list -> int

And in the module Arrayit has the type:

length: a' array -> int

But then I wanted you to use both modules in the same module that I implemented using the keyword open:

open List
open Array

When I tried to use the function lengthin the list, at compile time I had a type error.
Since OCaml is a strong statically typed language, I wonder why the compiler did not know that I needed the length function of the list module since I declared that I was using both.

+5
source share
1 answer

OCaml does not select one or the other function based on their types.

When you write

open Array

module functions Arraymask modules of the module Listwith the same name. When you later call a function length, OCaml looks for a function with a name length, finds Array.lengthand complains that this function does not have a compatible type.

- List.length ( length), .


, OCaml (.. , ), , .

+8

All Articles