[ocaml-platform] on the need and design of OCaml namespaces

Daniel Bünzli daniel.buenzli at erratique.ch
Tue Feb 26 15:40:53 GMT 2013


Le mardi, 26 février 2013 à 15:59, Didier Remy a écrit :
> Do you really need this level of granularity? I'd like to think of
> modules as the smallest compilation unit. Can you give us examples of what
> you'd like to do with value manipulation?

I have a (yet unreleased) library that provides basic types for computer graphics (vectors, matrices, sizes, quaternions, colors, boxes). I manually "packed" it as follows:

module Gg = struct
  module Float sig … end

  type m2  
  type m3  
  type m4

  (* Vectors *)
  type v2
  type v3
  type v4
  module type V (* implemented by all vector types *)
  module V2 : sig type t = v2 … end
  module V3 : sig type t = v3 … end
  module V4 : sig type t = v4 … end

  (* Points *)
  type p2 = v2
  type p3 = v3  
  module type P = sig … end (* implemented by all point types *)
  module P2 : sig type t = p2 … end
  module P3 : sig type t = p3 … end

  (* Matrices *)
  module type M = sig … end (* implemented by all matrix types *)
  module M2 : sig type t = m2 … end
  module M3 : sig type t = m3 ... end  
  module M4 : sig type t = m4 … end

  (* Quaternions *)
  type quat = v4  
  module Quat : sig … end

  (* Sizes *)  
  type size2 = v2  
  type size3 = v3  
  module Size2 = sig ... end
  module Size3 = sig … end

  (* Boxes *)
  ...

end

The idea was that people using the library would do an  

open Gg;;

at the top of their source. Now I would gladly replace that Gg with a namespace to maybe benefit of smaller executables (e.g. a 2D program that only uses V2, P2, M2). However at toplevel there are quite a few things that are not modules.  

1) Module types. These are mainly here to be able to functorize code over multiple dimensions.

2) Types. Are there for various reasons, first to avoid recursive module definition. Second so that printing of errors in the toplevel and by the compiler do not become long-winded/and or unreadable (e.g. print Gg.size2, Gg.v2 instead of Gg.Size2.t, Gg.V2.t).

3) (Values. I don't do that anymore but initially I also had value constructors for the types but I pushed these back into modules so that the open Gg guarantees you that no value is added to your environment, only types and modules are defined).  

So I guess in that case namespaces won't be of any help since only modules can be attached to them. The obvious solution would then to define all top level types maybe in a Gg.Types module, but I'd loose the short printing of type names and need to introduce artificial modules (Gg.Types).  

Daniel







More information about the Platform mailing list