reason - 无法理解复杂类型并对其进行解构-reasonml
问题描述
我使用 reason-apollo 从服务器获取数据。它返回我的数据类型(vscode 向我显示这种类型):
option(
Js.t(
< count : int;
rows : [ `User of
< firstName : string; id : string; lastName : string;
userName : string >
Js.t
| `Node of < id : string > Js.t ] option Js.Array.t >
)
)
我不太了解“行”的类型,也无法从中获取数据。我试过这个:
switch response##users {
| None => ReasonReact.string("none")
| Some(data) => {
data##rows |> Array.map(optionalRow => {
switch optionalRow {
| None => ReasonReact.string("none")
| Some(row) => ReasonReact.string(row##firstName);
}
});
ReasonReact.string("test");
}
};
但错误如下:
This has type:
array(option(Js.t(({.. firstName: string} as 'a)))) =>
array(ReasonReact.reactElement)
But somewhere wanted:
Js.Array.t(option([ `Node({. "id": string})
| `User({. "firstName": string, "id": string,
"lastName": string, "userName": string}) ])) =>
'b
The incompatible parts:
array(option(Js.t('a)))
vs
Js.Array.t(option([ `Node({. "id": string})
| `User({. "firstName": string, "id": string,
"lastName": string, "userName": string}) ]))
(defined as
array(option([ `Node({. "id": string})
| `User({. "firstName": string, "id": string,
"lastName": string, "userName": string}) ])))
Further expanded:
Js.t('a)
vs
[ `Node({. "id": string})
| `User({. "firstName": string, "id": string, "lastName": string,
"userName": string}) ]
如何从结果中获取“名字”?
解决方案
啊,很清楚,这是一个多态变体,这里是如何获取名字的片段。
...
switch optionalRow {
| None => ReasonReact.string("none")
| Some(row) => {
switch row {
| `User(u) => ReasonReact.string(u##firstName)
| `Node(n) => ReasonReact.string("test")
};
ReasonReact.string("test");
}
}
...
推荐阅读
- spring - 如何在春季为会话属性设置生存时间
- assembly - 'lretq' 指令有什么作用?
- oracle - 2 个用户同时运行存储过程 - 如果一次执行中的 DML 语句影响并行执行中的测试/条件会发生什么?
- javascript - 解析 DOM 以删除 x 的所有属性和 y 的所有类
- excel - 如何在特定日期获得比特币的澳元价格
- python - Python Pandas Groupby 计算单列中的唯一记录
- python - 当程序在正确的端口上侦听时,为什么“[Errno 61] Connection refused”,套接字绑定到所有接口?
- javascript - 在 JavaScript 中动态解析 JSON 数组
- proxy - 在 download_localhost 中使用 Kubespray,download_file 错误
- python - Python 3.7+ 规范是否保证 `collections.OrderedDict 不是 dict`?