The new cue.dev is live! Starting with v0.18, CUE Labs builds, signs and publishes CUE there, with v0.18 as our first long-term support release. For companies that depend on CUE, we're also introducing CUE Enterprise.
https://cue.dev/blog/the-cue-labs-distribution/
Changes which may break some users are marked below with::warning:
Language
As a reminder, the try experiment and the shortcircuit experiment introduced in v0.17.0 are both still ongoing. Please give them a try and report any issues or feedback!
:warning:Strict embedding and the ... operator
The explicitopen experiment, ongoing since v0.15.0, is now stable:from language version v0.18.0 on, embedding is strict and needs no opt-in. A struct which embeds a closed value is now closed to that value's fields, where before the embedding constrained only itself and left its sibling fields alone, and a comprehension's conjunct closes its struct just like any other conjunct. The new postfix ... operator opens an embedded value again, so #A... embeds #A without closing the struct around it.
#A:{a: int}
x:{
#A // x is now closed to #A's fields
b:1 // error: field not allowed
}
y:{
#A... // spread leaves y open
b:1
}
Migrate a module with cue mod upgrade, described below, or by running cue fix --exp=explicitopen while it is still at an older language version and only then raising language.version in cue.mod/module.cue:a file at v0.18.0 or later has no way back, as an @experiment attribute can only enable an experiment. See the how-to guide and the proposal for the full design.
:warning:Postfix aliases and self
The aliasv2 experiment, also ongoing since v0.15.0, is now stable:from language version v0.18.0 on, the postfix alias syntax and the predeclared self identifier need no opt-in, and the old prefix syntax is no longer accepted.
x~(X): {a: 1, b: X.a} // was x: X={a: 1, b: X.a}
[string]~(K,_):{name: K} // was [K=string]: {name: K}
// A value alias becomes a let binding on self.
y:{
let Y = self
c:2
d:Y.c // was y: Y={c: 2, d: Y.c}
}
As above, cue mod upgrade or cue fix --exp=aliasv2 rewrites the prefix form; cue fix --exp=explicitopen,aliasv2 applies both migrations in one pass. See the how-to guide and the proposal.
The new functions experiment
The new functions experiment adds function literals, function types, and calls to the language. A function literal declares its parameters, may constrain its result with ->, and defines its body after the final :; a call supplies arguments by position or by label. Functions are values:they unify with themselves and with function types, they capture the scope they are declared in, and they may be applied partially.
You can try this experiment on a module at language version v0.18.0 via the @experiment(functions) file attribute:
@experiment(functions)
add: func(a: int, b: int) -> int: a + b
scale:func(x: int) -> int: x * 2
out: add(1, scale(3)) // 7
A literal without a body denotes a function type, which constrains a function value in the same way a schema constrains a struct:an implementation unified with a function type must accept every argument the type admits, and callbacks are checked where they are passed. Function types match parameters by label, combining optional, required, and regular parameters as for struct fields. Recursion via closures, such as with a fixed-point combinator, is detected as a structural cycle (#4495). See the proposal discussion and its revision on function contracts for the full design.
The new openlists experiment
The new openlists experiment makes lists open by default, just like structs. A list literal without a trailing ellipsis is no longer fixed to its length outside of definitions, so unifying [3] with [_, 4] yields [3, 4] rather than an incompatible list lengths error. Lists close where structs close:within a definition, or when passed to close, which now accepts lists as well as structs.
The experiment also introduces closed literals:#[1, 2] means what [1, 2] means without the experiment, and #{a: 1} closes a struct at its own level as close does. As #[ now starts a closed list, an index on a definition named # is written (#)[i].
You can try this experiment on a module at language version v0.18.0 via the @experiment(openlists) file attribute. cue fix --exp=openlists rewrites existing files so they keep their meaning, writing list literals as closed literals and wrapping slices and builtin results in close. See the proposal for the full design.
:warning:Exact arithmetic
Integer addition, subtraction, and multiplication are now exact at any size, as the spec requires, rather than rounding to 34 digits. This also fixes strconv.FormatInt and uuid.FromInt failing on large integers (#3787, #4263), as well as SI multipliers on long literals, math.Floor, math.Ceil, and math.MultipleOf, which all rounded numbers beyond 34 digits.
Dividing two integers now yields an int when the quotient is exact, as the spec has always prescribed:4 / 2 is the integer 2, so x: float & 10 / 5 is now a conflict. A quotient also keeps the decimal places its operands imply, so 6.00 / 3 is 2.00, and an exported float always carries a digit after its decimal point.
The list.Sum, list.Product, list.Avg, list.Range, list.Min, list.Max, math.Abs, math.Copysign, and math.Dim functions now follow the arithmetic operators, keeping the kinds of their arguments:list.Sum([1.5, 2.5]) is now the float 4.0 rather than the integer 4, so a field declared int no longer accepts it.
Other changes
A CUE file which belongs to no module can now declare the language version it is written for with a @version attribute, placed ahead of any other file attribute and the package clause, so that a reproducer or a standalone file written for an older version runs as written without creating a module for it. The attribute is an error in a file of a module which declares a language version, as the module file decides the version of its files.
@version(v0.17.0)
package p
File attributes such as @experiment(...) and @extern(embed) are now honored between the package clause and the import declarations, where the spec allows them, rather than being silently ignored or rejected (#2371).
:warning:Pattern constraints may no longer be marked as optional or required: [string]?: int and [string]!: int are now rejected by the parser, matching the spec grammar. A pattern constraint is optional by construction, so the markers had no effect; simply remove them (#3716).
The comparison bounds == and != now work with boolean values, so constraints like !=true behave as documented in the spec (#4388). Multi-line strings now accept a backslash-escaped newline (#2317), and for k, k in x is rejected as a redeclaration (#4012).
Evaluator
:warning:Closedness
Selecting through a regular field which holds a definition now keeps the definition's closedness, just like selecting from the definition itself. Such selections inside a for comprehension's body are now closed as well, as they already were inside an if.
#D:s: {a: 1}
d:#D
x:#D.s & {extra: 1} // error: field not allowed
y:d.s & {extra: 1} // now also an error
This most often shows up when a comprehension iterates over a definition and the values it yields are then extended with further fields, for example by a pattern constraint. If those values are meant as data to build on, declare them as a regular or hidden field instead of a definition, or leave the struct open with ....
Closedness also follows what a reference denotes rather than where it is evaluated. A reference which reaches into a definition from outside now closes the value it selects even when it is evaluated inside that definition, such as outside.s below, which was previously accepted. Conversely, a definition which refers to a regular struct no longer rejects a field which a sibling reference in that struct supplies (#4501).
outside:#Out
let Out = {s:{a: 1}, u: outside.s & {b: 2}} // error: field not allowed
#Out:Out
#P:{a?: int}
x:{q: {}, s: #P & q & {a: 3}}
#L:{v: x} // no longer rejects a
As the spec requires, an ellipsis in an embedding, as in s:{{#B, ...}, h: {i: 1}}, now opens only the struct it is part of, and no longer the structs declared beside the embedding, so that a field added to one of them, such as s: h: extra: 1, is rejected as field not allowed. Give such a struct its own ellipsis, as in h: {i: 1, ...}, or declare the field in it. Fields opened by the spread operator are also closed again in more cases, as the spec requires (#4525, [#4528](https://cuelang.org/i