opa - 将用户定义的类型添加到数组

标签 opa

我定义了一种称为节点的类型以及节点列表。

type node = {name: string; description: string}
nodes = [] : list(node)

我创建了一个名为 createNewNode() 的函数,它创建一个新节点,将其分配给 selectedNode,并将其添加到数组节点中。

line 19: createNewNode() =
line 20:   selectedNode = {name="" remoteFSRoot=""} : node
line 21:   nodes = [nodes | selectedNode]
  ...

当我编译这个时,我收到以下错误:

Error
File "node.opa", line 21, characters 10-32, (21:10-21:32 | 592-614)
Expression has type { hd: list(node); tl: node } / 'c.a but is coerced into
list('a).
Types { name: string; description: string } and
{ hd: 'a; tl: list('a) } / { nil } are not compatible
Hint:
  One of the sum types may be missing the following cases of the
  other:
  { nil }
  { hd tl }.

此编译消息是什么意思以及如何修复它?

最佳答案

我认为您只是在第 21 行颠倒了 nodesselectedNode :

nodes = [selectedNode | nodes]

关于opa - 将用户定义的类型添加到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7364517/

相关文章:

opa - 保护 String.to_int 和其他

ide - 用于 Opa 编程的 IDE 以及如何托管 Opa 代码?

ocaml - opa 扩展可以用 Ocaml 编写吗?

string - 在 rego 中将 2 个字符串连接在一起

go - 无法加载 github.com/open-policy-agent/opa/capability : no Go source files

Opa:提交表单时防止页面重新加载

opa - 是否可以在 Opa 中创建全局变量?

opa - 如何让 SimpleTable 组件工作?