CUE v0.15.0-alpha.1 introduced the “explicitopen” experiment, which greatly simplifies CUE’s concept of closedness.
cue fix --exp=explicitopen
to update its code and enable the experiment on a per-file basis.
See
cue help fix
and
cue mod edit --language-version
for more information.
The trailing ... operator
Using the trailing ... operator explicitly opens the reference to the closed
struct that it follows:
@experiment(explicitopen) // Enable the experiment.
package example
// The trailing ... operator prevents #Schema's
// closedness being "inherited" by X, and permits
// more fields to be added to X.
X: #Schema...
X: {
A: "closed?"
B: "no!"
}package example
#Schema: {
A: string
}$ cue export --out yaml
X:
A: closed?
B: no!Closedness when embedding
A closed struct now closes the struct in which it’s embedded –
unless it’s followed by the trailing ... operator.
This means that a schema can now close the top-level of a package:
@experiment(explicitopen) // Enable the experiment.
package example
// Only regular fields mentioned in #Schema are
// now permitted at the top-level of this package.
#Schema
A: "closed?"
B: "yes!"package example
#Schema: {
A: string
}$ cue vet -c
B: field not allowed:
./file.cue:8:1As a result, enabling the experiment means that embedding a value is now exactly equivalent to unifying with that value – and therefore these two CUE snippets are now semantically identical:
@experiment(explicitopen)
A: {
B
c: "foo"
}@experiment(explicitopen)
A: B & {
c: "foo"
}Conclusion
The “explicitopen” experiment simplifies how CUE handles closedness, allowing for
more flexibility and control when closed structs are referenced. This
flexibility now permits the top-level of packages to be closed, allowing them
to be fully validated against schemas without permitting additional fields.
Use cue fix --exp=explicitopen to update existing CUE automatically.
As with all CUE language experiments, one of this experiment’s aims is to gather feedback from users. Please do join the CUE community and tell us about your experience with the experiment!
Related content
- Reference: cue help experiments – a list of the language experiments that can be enabled or disabled
- Reference: cue help fix – update CUE code automatically, applying the latest fixes and language experiments
- Reference: cue help mod edit – update the language version targeted by a module