1

我从 js 端得到了一个数据,看起来像这样 { "selectionSet": { "type": 1, "selections": [ { "name": { "kind": "Name", "value": "viewer" }, "selectionSet": { "type": 1, "selections": [ { "name": { "kind": "Name", "value": "avatarUrl" }, "selectionSet": null } ] } } ] } } 我想知道如何为selectionSetand定义类型selections

似乎在定义时selections,我应该定义,selectionSet因为它有一个类型为的字段selectionSet。但是当定义时selectionSet,我应该定义selections

有人可以用 OCaml 风格回答吗?我想将此 JSON 样式数据转换为记录。

4

1 回答 1

3

要为此定义相互递归类型或函数,您可以使用关键字and. 在您的情况下,您的记录看起来像这样:

type selectionSet = {
  t : t;
  selections : selections list;
}

and selections = {
  name : name;
  selectionSet : selectionSet option;
}
于 2018-05-17T06:21:44.640 回答