Skip to Content
Language in depth

Values

The kinds of value, structs with var fields, enums and exhaustive match, literal unions, and the conversions CP Lang never makes for you.

CP Lang has a small set of value kinds, and every one of them behaves the same way wherever it appears: in a local, an array, a field or a function argument. This chapter is the map; the normative rules are values and types.

The kinds of value

Every value is one of four kinds. (core/02-values-types §2.1)

KindExamplesOn assignment
plainnumbers, Bool, Char, positions and cuts, ?T of a plain T, tuples, records, structs and enums built from plain valuescopies
heapList, Str, [D]T, Map, Set, every container, any struct holding onemoves; .clone() copies
functionfn items, lambdas, operation valueszero size when capturing nothing, always inlined
generatora recipe that pushes elements to a loop bodyconsumed by the loop

Whether a value is owned or lent is not part of its type; that's the ownership and mutation chapter. Numbers, range types and their widths are types, widths and proofs, and ?T is optionals.

Structs

A struct's fields are fixed after construction unless declared var, the same split as let and var. A field can be typed by its default, the constructor is generated, and methods that change the struct take mut self:

Output
Press Run (Ctrl+Enter).

Assigning a field that isn't var is an error naming the field, with both fixes:

error on purposeOpen in playground
Output
Press Run (Ctrl+Enter).

Records, (lo: Int, hi: Int), are the anonymous form: structural, with named fields, and no declaration. (core/02-values-types §2.6)

Enums and match

An enum's variants are PascalCase, like Some and None, and may carry fields. read() reads a variant by its name, so a query stream is one line, and match must cover every variant:

Output
Press Run (Ctrl+Enter).

Drop the Ask arm and the program doesn't compile:

error on purposeOpen in playground
Output
Press Run (Ctrl+Enter).

A payload-free enum, enum Dir = L | R | U | D, is a domain: [Dir]Int is a table and for d in Dir walks it. (core/02-values-types §2.7)

Literal unions

When a statement says an operation is one of a few tokens, the type says it too. "dep" | "wd" has exactly those two values: the read rejects anything else, and match is exhaustive over them.

Output
Press Run (Ctrl+Enter).

Never implicit

No numeric conversion happens without being written, except into a wider range of the same type, T into ?T, and Int into Mod<P>. F64 to Int names its rounding:

error on purposeOpen in playground
Output
Press Run (Ctrl+Enter).

The help offers floor(x), ceil(x), round(x) or trunc(x). There's also no implicit copy of a heap value, no implicit Int to position, no inheritance, no subtyping, no overloading by argument type and no user-defined implicit conversion. (core/02-values-types §2.11)

Inference is local, too: it unifies the parts of one expression and never looks at later statements. var best = None followed by best = 3 is an error at the declaration, asking for var best: ?Int = None, so the error points at the line that needs the fix.

error on purposeOpen in playground
Output
Press Run (Ctrl+Enter).

Why it's designed this way

var fields. decision 136 mirrors let and var on fields, so a reader sees which parts of a struct can change, and cpl fix adds var to every field a program assigns. The alternative, every field mutable, hides which state a struct's methods maintain.

Patterns match only the stored shape. decision 132 rejected user-defined patterns (Scala's unapply, F#'s active patterns). A custom pattern runs arbitrary code while looking like a free field read, defeats exhaustiveness checking, and needs a matcher protocol through the checker, LSP and trace, all to save one method call. A view is a method returning an enum.

Local inference. The alternatives were C++'s rule (every declaration spells its type) and whole-function inference in the style of Hindley-Milner, where var x = None takes its type from a later x = 3. The first costs tokens on every line; the second puts an error at the line where two guesses finally collide, often far from the declaration to change, and its cost grows with the function. decision 131 takes the middle: siblings within one expression (list literals, if branches, match arms) are unified, so [Pair(1, None), Pair(2, 3)] infers ?Int, but nothing looks at later statements. Every error points at the line to change, and checking stays linear.

No overloading. C++ and Java resolve a call among overloads by argument type, which means a reader has to know every argument's exact type to know which function runs, and a failed call lists every candidate. decision 127 gives one name one generic signature. The cases overloading would cover become named arguments (sort(by:), at:) or a trait the arguments implement, both of which a reader sees at the call.

Copies are explicit. decision 128 makes let b = a on a list, map, string or struct holding one a move when it is a's last use, and an error asking for a.clone() otherwise. The rejected alternatives were C++'s silent deep copy, which makes an innocent-looking line O(n), and Python's shared reference, where a change through b shows up in a. An explicit .clone() keeps the cost visible and makes aliasing impossible to write by accident.

Exact conversions. Each implicit conversion C++ makes is a known bug source: int to double and back truncating, size_t mixing with signed values, a long long narrowed into an int field. Requiring the conversion in the source costs one word and keeps the rounding or range check where it can be seen.

On this page