Requires CUE v0.15.0 or later

CUE v0.15.0-alpha.2 introduced the “aliasv2” experiment, which replaces alias syntax with a more consistent form.

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 alias V referring to the value of X
  • X~(L,V) – the dual form creates alias L referring to the label of X and alias V referring to the value of X
  • self – 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:

current-syntax.cue
// 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
experimental-syntax.cue
@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: 48

Because 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:

TERMINAL
$ 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
TERMINAL
$ 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: 48

The experimental syntax makes some aliasing scenarios possible for the first time, where the current syntax does not provide a direct equivalent:

experimental-syntax.cue
@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]
TERMINAL
$ 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
  - 22

Conclusion

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!