代码之家  ›  专栏  ›  技术社区  ›  ThomasThiebaud

如何将整个模块绑定为函数?

  •  0
  • ThomasThiebaud  · 技术社区  · 7 年前

    debug 为了学习。我有这个密码

    module Instance = {
      type t;
      external t : t = "" [@@bs.module];
    };
    
    module Debug = {
      type t;
      external createDebug : string => Instance.t = "debug" [@@bs.module];
    };
    

    我试着这样使用它

    open Debug;    
    let instance = Debug.createDebug "app";
    instance "Hello World !!!";
    

    Error: This expression has type Debug.Instance.t
           This is not a function; it cannot be applied.
    

    不是的 instance

    module Instance = {
      type t;
      external write : string => unit = "" [@@bs.send];
    };
    

    open Debug;    
    let instance = Debug.createDebug "app";    
    instance.write "Hello World !!!";
    

    Error: Unbound record field write
    

    1 回复  |  直到 7 年前
        1
  •  1
  •   ivg    7 年前

    这个 createDebug 函数,根据您的声明,返回类型为的值 Instance.t 实例.t 价值和 Debug.createDebug

    您的第二个示例证明您正在考虑模块,因为它们是一种运行时对象或记录。但是,它们只是静态结构,用于将大型程序组织到分层名称空间中。

    您试图使用的实际上是一个记录:

    type debug = { write : string => unit }
    
    let create_debug service => {
      write: fun msg => print_endline (service ^ ": " ^ msg)
    }
    
    let debug = create_debug "server"
    debug.write "started"
    

    将产生:

    server: started