Constants
A constant is a named, compile-time value.
NAME— a plain identifier (not scoped).Type— any type; in practice a primitive.value— one of:- a literal: an integer,
true/false, or a"quoted string"(plain, no interpolation) - another constant, by name —
WORLD_COUNT, orpkg::mod::DEFAULTfor one another schema declares - an integer type's bound —
u32::MIN,u64::MAX
- a literal: an integer,
A value that names a constant or a bound is checked as the value it comes to. It has to be the constant's kind of value (an integer is also a fine float) and, for an integer, fit the declared width:
const LIMIT: u32 = 500
const SMALL: u8 = LIMIT // error: `LIMIT` is 500, which does not fit in `u8`
const TOP: u64 = u64::MAX // fine
A name that isn't a constant is an error, and so are constants that name each other in a loop. Generated code holds the value a name comes to, as if the literal had been written.
A constant can carry a docstring:
Using one
A constant is referenced by name wherever a value is written: a struct's or an error's field default, a validator property's default, or a validator's argument. It's checked there the same way, against the type it's written for:
Sizes from constants
An array's size can be a constant, in a maximum or an exact length:
const NAME_MAX: u8 = 32
const LIMIT: u32 = 500
struct Customer {
name: char[max NAME_MAX]
code: char[NAME_MAX]
orders: Order[max LIMIT]
chunk: u8[max u16::MAX]
}
- It names an unsigned integer constant: declared
u8,u16,u32oru64, in this schema or another (pkg::mod::NAME_MAX), or an unsigned type's bound such asu16::MAX. A signed or non-integer constant, or a name that isn't a constant, is a build error saying so. - It is stored resolved. The stored schema holds the number, so the maximum is the same wherever the constant came from, and changing the constant changes the type, which is a breaking change like any other bound change.
- The same limits apply: at least 1 and at most
u32::MAX, so a constant of0is an error where it is used as a size.
See Bounds for what a maximum means.