【问题标题】:Why is my Map type not exported by Reason?为什么我的 Map 类型没有被 Reason 导出?
【发布时间】:2018-02-27 01:00:38
【问题描述】:

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);),那么它就可以工作。

我在这里错过了什么?

【问题讨论】:

  • 原来empty 生活在StringMap,而不是scores。我觉得这很令人惊讶。确定要创建Map 实例,必须知道键类型?

标签: dictionary module ocaml reason reasonml


【解决方案1】:

scores 是一个类型,类型,甚至记录类型,在类型本身上没有字段。此外,类型和值存在于不同的命名空间中,因此虽然 scores type 存在,但 scores value 不存在,因此错误“值分数可以”不能在分数中找到”。

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

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

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

【讨论】:

    猜你喜欢
    • 2015-05-10
    • 1970-01-01
    • 2023-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 2019-05-27
    相关资源
    最近更新 更多