Constants
const name : T := value; outside a function declares a constant, written like a local binding. Its value is known when the contract compiles, and every use compiles to the literal: numbers, hashes, addresses, and the length of a fixed list.
constants.lithe
(* A constant compiles to its literal wherever it is named. *)
import "std/address";
import "std/crypto";
import "std/int";
import "std/uint";
const maxFeeBps : uint := 1_000;
const denominator : uint := 10 * maxFeeBps;
(* The hash of a literal is a constant too. *)
const pauserRole : bytes32 := Crypto.keccak256("PAUSER_ROLE");
(* A constant can be a fixed list's length, or a loop's bound. *)
const maxAdmins : uint := 3;
storage is record {
feeBps : uint;
admins : list(address, maxAdmins);
}
error FeeTooHigh of uint;
external function setFee(const feeBps : uint)
raises FeeTooHigh
is {
assert(feeBps <= maxFeeBps, FeeTooHigh(feeBps));
storage.feeBps := feeBps;
}
external view function feeOn(const amount : uint) : uint is
amount * storage.feeBps / denominator
external pure function sum(const xs : list(uint)) : uint is {
var total : uint := 0;
for x : uint in xs bound maxAdmins * 2 do {
total += x;
}
} with total
(* Built-in constants take their width from context. *)
external pure function limits() : (uint, uint16, int, address) is
(Uint.max, Uint.max, Int.max, Address.zero)1_000uses_as a digit separator.- A constant's value is literals, other constants, and operators, casts, and hashes of literals over those. Anything else, such as a storage read or a call with arguments, is a compile error.
Crypto.keccak256("PAUSER_ROLE")compiles to the 32-byte hash, so a role id or an EIP-712 typehash is written as its preimage, not pasted in.- A fixed list's length and a loop's
boundcan be a constant,M.nfrom a module, or arithmetic over those:list(address, maxAdmins),bound maxAdmins * 2. - Arithmetic on constants is checked when the contract compiles:
maxFeeBps * Uint.maxis a compile error, not a panic at run time. - A constant is never part of the ABI. To publish one, return it from an
external pure function. - A module's constants are named
M.noutside it. A local cannot take a constant's name. Uint.maxistype(uint256).max, or the maximum of whatever width the context expects. Herelimitsreturns it both as auintand as auint16.

