emchristiansen
emchristiansen

Reputation: 3612

OCaml: How do I provide fully-qualified module names to avoid name collisions?

I'm working in a package Foo with a module called Base, but I'd also like to use the OCaml library Base. How can I distinguish between the two in open statements?

E.g., is there a module-root specifier like in other languages, so I could type open __root__.Base or open __root__.Foo.Base? E.g. in C++ I could type ::Base or ::Foo::Base, where the leading :: indicates the name is fully qualified starting at root.

Upvotes: 0

Views: 293

Answers (2)

Chris
Chris

Reputation: 36660

Even after opening a module, you can still refer to it using the fully qualified name.

# module A = struct 
    module C = struct 
      let d = 42 
    end 
  end ;;
module A : sig module C : sig val d : int end end
# module B = struct 
    module C = struct 
      let d = 27.3 
    end 
  end ;;
module B : sig module C : sig val d : float end end
# open A
  open B ;;
# open C ;;
# d;;
- : float = 27.3
# A.C.d ;;
- : int = 42
# B.C.d ;;
- : float = 27.3

Upvotes: 1

ivg
ivg

Reputation: 35280

First of all, you don't need to open a module to use it. So just refer to them as Foo.Base and Base respectively.

If you still want to open a module that shadows some definition, then the common practice is to rename the shadowed module before the the open statement, e.g.,

module B = Base
open Foo

Upvotes: 3

Related Questions