Exploring ReasonML 学习笔记 -- 2. Module

201 阅读1分钟

Exploring ReasonML

  1. Hide value in module

By default, every module, type and value of a module is exported. If you want to hide some of these exports, you must use interfaces. Additionally, interfaces support abstract types (whose internals are hidden).

The convention, borrowed from OCaml, is to use the name t for the main type supported by a module.

  1. Importing values from modules

    1. Opening modules

    2. Including modules: Then all of its exports are added to the exports of the current module.

    3. Including interfaces

      module type InterfaceA = {
        ···
      };
      
      module type InterfaceB = {
        include InterfaceA;
        ···
      }
      
      module type LogWithDateInterface = {
        include (module type of Log); /* A */
        let logStrWithDate: (t, t) => t;
      };
      
      module LogWithDate: LogWithDateInterface = {
        include Log;
        ···
      };
      
  2. Namespacing modules

    proj/
        foo/
            NamespaceA.re
            NamespaceA_Misc.re
            NamespaceA_Util.re
        bar/
            baz/
                NamespaceB.re
                NamespaceB_Extra.re
                NamespaceB_Tools.re
                NamespaceB_Util.re
    
    /* NamespaceA.re */
    module Misc = NamespaceA_Misc;
    module Util = NamespaceA_Util;