val: 42A: { val: 23 num: val // will be 23, matches locally// irregular names need to be quoted and require indexing to access"user-id": "abc" UserID: A["user-id"]}A: { b: val // 42, matches top-level c: A.num // 23, reference A.num// num itself is not in scope even though it is part of A}
// a mathematically valid releationship// but invalid in Cue until one or the other has a valuea: b -10b: a +10// we need to set a or b and Cue will do the rest// if not, a cycle error will be reporteda: 100
构造循环
CUE 不允许存在构造循环和递归,可以定义无限的结构只要数据是有限的。
structural.cue
#List: { val: _ next: #List |*null}// Cannot do this, it is structural recursion#Contains: { list: #List val: _ found: bool|*falseif list.val == val { found: true }if list.val != val && list.next !=null {// No recursion! found: #Contains & {"list": list.next, "val": val} }}// We can define an infinite structurelist: #List & {val: "a", next: {val: "b"}}// results inlist: { val: "a" next: { val: "b" next: null }}