Requires CUE v0.16.0 or later
CUE v0.16.0-alpha.2 introduced the “try” experiment, which also adds
new optional else and fallback clauses in comprehensions.
To use this language feature, update your module to target language version v0.16.0
or later with cue mod edit:
TERMINAL
$ cue mod edit --language-version v0.16.0You can now enable the experiment on a per-file basis using @experiment(try).
With this change to the language, an if comprehension may be followed
by an else clause which triggers when the condition is not met,
and a for comprehension may be followed by a fallback clause
which triggers when the comprehension produced zero values.
An else clause can help avoid repetition or verbosity, for instance:
if-without-else.cue
// No experiment required
package p
_foo: true
_bar: true
if _foo && _bar {
withoutElse: "condition met"
}
if !(_foo && _bar) {
withoutElse: "condition not met"
}if-with-else.cue
@experiment(try)
package p
if _foo && _bar {
withElse: "condition met"
} else {
withElse: "condition not met"
}TERMINAL
$ cue export --out yaml
withElse: condition met
withoutElse: condition metA fallback clause can be used for loops which can produce zero values:
for-with-fallback.cue
@experiment(try)
package p
_inputs: ["foo.txt", "bar.xml", "baz.toml"]
for i, name in _inputs
if name =~ #"\.cue$"# {
cueInputs: (name): i
} fallback {
cueInputs: "fallback.cue": -1
}TERMINAL
$ cue export --out yaml
cueInputs:
fallback.cue: -1