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

为什么我的地图类型没有按原因导出?

  •  1
  • sdgfsdh  · 技术社区  · 7 年前

    下列的 this question ,我创建了一个文件(以及相应的模块),该文件定义了 Map 类型:

    /* Scores.re */
    module StringMap = Map.Make({
      type t = string;
      let compare = compare
    });
    
    type scores = StringMap.t(int);
    

    现在,我想在另一个文件中使用该类型:

    /* Demo.re */
    let currentScores = Scores.scores.empty;
    
    Js.log(currentScores);
    

    但是,这给了我一个错误:

    The value scores can't be found in Scores
    

    如果我添加一个常量(例如。 let n = 123; Js.log(Scores.n); )然后它就起作用了。

    我错过了什么?

    1 回复  |  直到 7 年前
        1
  •  2
  •   glennsl Namudon'tdie    7 年前

    scores 是一个类型,而类型,甚至记录类型,在类型本身上都没有字段。此外,类型和值位于不同的名称空间中,因此 分数 类型 存在,则 分数 价值 没有,因此出现错误“分数中找不到数值分数”。

    另一方面,模块可以有“字段”,这就是它存在的原因。当然你也可以用别名 empty 就像你给 Scores.t 类型:

    type scores = StringMap.t(int);
    let empty = StringMap.empty;
    

    最后,您会问“确定要创建映射实例,必须知道密钥类型吗?”。确实如此,而且你已经让大家知道了。您在创建 StringMap 模块( Map.Make({ type t = string; ...0); ). 无需指定值类型( int )但是。这将被推断出来。