【问题标题】:How to define mutually recursive types如何定义相互递归的类型
【发布时间】:2018-03-28 05:27:30
【问题描述】:

我有以下代码:

type cellInfo('a) = {
  index: int,
  column: column('a),
  id: string
};

type cellInfoFn('a) = cellInfo('a) => ReasonReact.reactElement;

type column('a) = {
  header: string,
  accessor: accessor('a),
  id: option(string),
  cell: cellInfoFn('a),
};

看看每种类型如何相互依赖。

我收到以下错误消息:

[1]   12 │ type cellInfo('a) = {
[1]   13 │   index: int,
[1]   14 │   column: column('a),
[1]   15 │   id: string
[1]   16 │ };
[1]
[1]   This type constructor's parameter, `column`, can't be found. Is it a typo?

如何处理相互递归的类型定义?

【问题讨论】:

    标签: recursion ocaml typing reason


    【解决方案1】:

    相互递归的定义必须用and 分隔。这适用于let 定义和type 定义:

    type cellInfo('a) = {
      index: int,
      column: column('a),
      id: string
    }
    
    and cellInfoFn('a) = cellInfo('a) => ReasonReact.reactElement
    
    and column('a) = {
      header: string,
      accessor: accessor('a),
      id: option(string),
      cell: cellInfoFn('a),
    };
    

    【讨论】:

    • 这看起来有效。谢谢!这可能吗/跨多个文件看起来如何?
    • 不,它们必须在同一个文件中定义。处理文件间相互递归的一种常见做法是将它们放在同一个文件中,然后将它们别名回它们“所属”的位置。
    • 这也适用于模块吗?我想它们可以私下定义并在以后放入模块中。
    • 只要模块在同一个文件中,它就可以工作。您还必须使用 rec 关键字,就像 let 一样,并为每个模块提供签名。
    猜你喜欢
    • 2020-08-11
    • 1970-01-01
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-05
    • 2011-03-13
    • 1970-01-01
    相关资源
    最近更新 更多