CUE v0.15.0-alpha.2 introduced the “aliasv2” experiment, which replaces alias syntax with a more consistent form.
cue fix --exp=aliasv2
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 experiment replaces CUE’s current alias syntaxes
with a unified syntax using the ~ operator in one of two forms,
and defines the self indentifier:
X~V– the single form creates aliasVreferring to the value ofXX~(L,V)– the dual form creates aliasLreferring to the label ofXand aliasVreferring to the value ofXself– refers to the innermost CUE block that contains the identifier
The new syntax can be used with fields, pattern constraints, and dynamic fields. Each of CUE’s current alias syntaxes has a direct replacement, as outlined in the experiment’s proposal.
Here are some examples of aliases using the current syntax, and their
experimental syntax as rewritten by cue fix:
// Refer to a field whose name is
// not a valid identifier.
A="-foo": 42
b: A + 1
// Refer to a field's label.
c: [L=string]: {
id: L
}
c: foo: _
// Refer to a field's value.
_d: V={
e: V.g + 1
}
d: _d & {
g: 44
}
// Refer to a field inside a pattern constraint.
h: PCF=[string]: {
i: PCF.j + 1
}
h: foo: j: 46
// Refer to a value inside a pattern constraint.
k: [string]: PCV={
m: PCV.p + 1
}
k: foo: p: 48@experiment(aliasv2)
// Refer to a field whose name is
// not a valid identifier.
"-foo"~A: 42
b: A + 1
// Refer to a field's label.
c: [string]~(L,_): {
id: L
}
c: foo: _
// Refer to a field's value.
_d: {
let V = self
e: V.g + 1
}
d: _d & {
g: 44
}
// Refer to a field inside a pattern constraint.
h: [string]~PCF: {
i: PCF.j + 1
}
h: foo: j: 46
// Refer to a value inside a pattern constraint.
k: [string]: {
let PCV = self
m: PCV.p + 1
}
k: foo: p: 48Because we used cue fix to rewrite current-syntax.cue as experimental-syntax.cue,
we can be sure that these examples still define the same concrete data:
$ cue export current-syntax.cue --out yaml
-foo: 42
b: 43
c:
foo:
id: foo
d:
e: 45
g: 44
h:
foo:
i: 47
j: 46
k:
foo:
m: 49
p: 48$ cue export experimental-syntax.cue --out yaml
-foo: 42
b: 43
c:
foo:
id: foo
d:
e: 45
g: 44
h:
foo:
i: 47
j: 46
k:
foo:
m: 49
p: 48The experimental syntax makes some aliasing scenarios possible for the first time, where the current syntax does not provide a direct equivalent:
@experiment(aliasv2)
// Pattern constraint aliases make a field (F) and
// its label (L) available within the scope of the
// field's value.
a: [string]~(L,F): {
b: F.c + 1
id: L
}
a: foo: c: 42
"-foo": 44
// The self identifier grants access to top-level
// fields whose names are not valid identifiers.
d: self["-foo"] + 1
// Binding an identifier to the top level of the
// file grants access to top-level fields that
// would be shadowed, or whose names are not valid
// identifiers.
let Root = self
e: {
g: Root["-foo"] + 2
"-foo": "not-44"
}
// The self identifier references containing lists.
h: [10, self[0] + 1, self[1] * 2]$ cue export experimental-syntax.cue --out yaml
a:
foo:
b: 43
id: foo
c: 42
-foo: 44
d: 45
e:
g: 46
-foo: not-44
h:
- 10
- 11
- 22Conclusion
Combining the ~ operator with the self identifier, the “aliasv2” experiment
replaces all of CUE’s different alias forms with a unified and consistent
syntax. Each of their specific replacements are outlined in
the experiment’s proposal.
Use cue fix --exp=aliasv2 to apply the replacements to 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
- Equivalent current and experimental syntaxes – each of CUE’s alias forms expressed as its experimental equivalent