ocaml - 如何为嵌套递归数据定义数据类型
问题描述
我从 js 端得到了一个数据,看起来像这样
{
"selectionSet": {
"type": 1,
"selections": [
{
"name": {
"kind": "Name",
"value": "viewer"
},
"selectionSet": {
"type": 1,
"selections": [
{
"name": {
"kind": "Name",
"value": "avatarUrl"
},
"selectionSet": null
}
]
}
}
]
}
}
我想知道如何为selectionSet
and定义类型selections
。
似乎在定义时selections
,我应该定义,selectionSet
因为它有一个类型为的字段selectionSet
。但是当定义时selectionSet
,我应该定义selections
有人可以用 OCaml 风格回答吗?我想将此 JSON 样式数据转换为记录。
解决方案
要为此定义相互递归类型或函数,您可以使用关键字and
. 在您的情况下,您的记录看起来像这样:
type selectionSet = {
t : t;
selections : selections list;
}
and selections = {
name : name;
selectionSet : selectionSet option;
}
推荐阅读
- java - Spring批处理在启动运行方法之前启动SimpleJobLauncher运行方法
- spring - 如何在 Spring 集成测试中模拟注入服务的方法
- html - 将 Clickfunnel 导出/下载为 HTML(包括所有资产)
- html - 如何在所有浏览器中获得统一的 unicode(HTML 实体)外观
- vb.net - 500k kvps的并发字典(是否删除kvps)
- css - 如何防止图像在较低分辨率下过大?
- c# - 将表格拆分为 PowerPoint 文件中的多张幻灯片
- android - onStop 在 onDestroy 之后被调用?
- javascript - 用于渲染 Dicom 的 vtk.js 和基石交互
- c# - 如何确定使用哪种方法,在 ASP.NET Core 中使用我的自定义属性