github.com/solo-io/cue@v0.4.7/doc/tutorial/basics/6_expressions/80_coalesce.txt (about) 1 cue eval coalesce.cue 2 cmp stdout expect-stdout-cue 3 4 -- frontmatter.toml -- 5 title = "Null Coalescing" 6 description = "" 7 8 -- text.md -- 9 <!-- jba: the terms here are confusing. "Null coalescing" is actually not 10 that, but then there is something called "actual null coalescing." 11 12 Just say that because _|_ | X evaluates to X, you can use disjunction 13 to represent fallback values. 14 15 And then you can use that to effectively type-check with a default value. 16 --> 17 18 With null coalescing we really mean error, or bottom, coalescing. 19 The defaults mechanism for disjunctions can also be 20 used to provide fallback values in case an expression evaluates to bottom. 21 22 In the example the fallback values are specified 23 for `a` and `b` in case the list index is out of bounds. 24 25 To do actual null coalescing one can unify a result with the desired type 26 to force an error. 27 In that case the default will be used if either the lookup fails or 28 the result is not of the desired type. 29 30 -- coalesce.cue -- 31 list: [ "Cat", "Mouse", "Dog" ] 32 33 a: *list[0] | "None" 34 b: *list[5] | "None" 35 36 n: [null] 37 v: *(n[0]&string) | "default" 38 39 -- expect-stdout-cue -- 40 list: ["Cat", "Mouse", "Dog"] 41 a: "Cat" 42 b: "None" 43 n: [null] 44 v: "default"