Keywords Reference
Every keyword in Compact, organized by purpose. Use this as a lookup when you’re unsure what a keyword does or whether it’s the right tool.
Docs: Compact Keywords
Intuition First
Compact has fewer keywords than TypeScript, no class, function, interface, let, var, null, this, while. What remains is lean and purposeful.
The keywords fall into four groups:
- Declaration keywords, define things:
circuit,struct,enum,ledger,witness,module,type,new type - Expression keywords, operate on values:
as,assert,disclose,default,map,fold,pad,slice - Control keywords, manage flow:
if,else,for,return,const - Module keywords, organize code:
export,import,include,pragma,prefix,from
A few TypeScript keywords are reserved for future use (await, class, interface, let, null, etc.) and cannot be used as identifiers. Identifiers starting with __compact are reserved for the compiler.
Module and Visibility
| Keyword | What it does | Example |
|---|---|---|
export | Makes a binding visible outside its scope. At top level, marks entry points callable from TypeScript. | export circuit f(): T { } |
module | Defines a named namespace. Contains program elements. | module M { export circuit f() {} } |
import | Brings exported bindings from a module into scope. Also imports the built-in stdlib. | import M; / import CompactStandardLibrary; |
from | Used in selective import to name the source module. | import { f } from M; |
prefix | Attaches a namespace prefix to all imported names. | import Math prefix M$; |
include | Splices the contents of another file inline at the current location. | include "shared/types.compact"; |
pragma | Declares a constraint on compiler or language version. | pragma language_version >= 0.22; |
Declaration Keywords
Types and Data Structures
| Keyword | What it does | Example |
|---|---|---|
struct | Declares a named record type with typed fields. Nominal typing. | struct Point { x: Field, y: Field } |
enum | Declares a named sum type. First variant is the default. | enum State { UNSET, SET } |
type | Declares a structural type alias, fully interchangeable with the underlying type. | type Hash = Bytes<32>; |
new type | Declares a nominal type alias, distinct from the underlying type. Requires explicit cast. | new type UserId = Bytes<32>; |
Opaque | Declares a foreign JavaScript value opaque to circuits. Currently "string" and "Uint8Array". | Opaque<"string"> |
Boolean | The boolean type. Values: true and false. | const flag: Boolean = true; |
Field | Unsigned integer up to the ZK field order. Comparisons only via == and !=. | const f: Field = 42; |
Uint | Unsigned integer. Sized (Uint<n>) or bounded (Uint<0..n>). | Uint<64>, Uint<0..256> |
Bytes | Fixed-length byte vector. Requires size argument. | Bytes<32> |
Vector | Homogeneous fixed-length sequence. Shorthand for same-type tuple. | Vector<4, Field> |
Ledger and Witnesses
| Keyword | What it does | Example |
|---|---|---|
ledger | Declares a field in the on-chain public state. | ledger count: Counter; |
export ledger | Makes a ledger field readable from TypeScript. | export ledger owner: Bytes<32>; |
sealed ledger | Makes a ledger field writeable only during initialization. | sealed ledger config: Uint<32>; |
witness | Declares a private-input callback. Body is in TypeScript. | witness secretKey(): Bytes<32>; |
constructor | Defines the initialization function, called once on deployment. | constructor(v: Field) { } |
Circuits
| Keyword | What it does | Example |
|---|---|---|
circuit | Defines a named circuit, compiles to a ZK circuit. | circuit add(a, b): Field { } |
export circuit | Makes a circuit callable from TypeScript (entry point). | export circuit mint(): [] { } |
pure | Asserts a circuit has no side effects, no ledger reads/writes, no witness calls. | pure circuit hash(v: T): Bytes<32> { } |
contract | Reserved for declaring external contract types. Not fully implemented in 1.0. |
Expression Keywords
Operations on Values
| Keyword | What it does | Example |
|---|---|---|
as | Type cast. Widens, narrows, or converts between compatible types. | x as Field, bytes as UserId |
assert | Runtime guard. Halts the circuit with an error message if the condition is false. | assert(balance >= amount, "Insufficient") |
disclose | Compile-time annotation. Declares that witness data is intentionally going public. | disclose(witness()) |
default | Produces the default value of a type. | default<Bytes<32>> |
pad | Pads a string literal with zero bytes to a fixed length. Produces Bytes<n>. | pad(32, "midnight:example") |
map | Higher-order transformation over a vector or tuple. | map((x) => x + 1, v) |
fold | Higher-order accumulation over a vector or tuple. | fold((acc, x) => acc + x, 0, v) |
slice | Extracts a contiguous sub-vector of a given size from a larger vector. | slice<4>(v, start) |
Literals and Special Forms
| Keyword | What it does | Example |
|---|---|---|
true | Boolean true value. | const flag = true; |
false | Boolean false value. Default value of Boolean. | const flag: Boolean = false; |
Control Flow
| Keyword | What it does | Example |
|---|---|---|
if | Conditional branching. One-armed (if (cond) { }) or two-armed (if (cond) { } else { }). | if (state == SET) { } |
else | The alternative branch of a two-armed if. | if (a) { } else { } |
for | Bounded iteration. Over a numeric range or a vector/tuple. | for (const i of 0..10) { } |
of | Used in for loop to name the iteration variable. | for (const x of vec) { } |
return | Exits a circuit or constructor and returns a value. Cannot be used inside a for body. | return x; |
const | Declares an immutable local variable binding. | const x = 42; |
Reserved for Future Use
These TypeScript/JavaScript keywords are reserved, they cannot be used as identifiers in Compact 1.0 but may have meaning in future versions:
await, break, case, catch, class, continue, debugger, delete, do, extends, finally, function, implements, in, instanceof, interface, let, null, package, private, protected, public, static, super, switch, this, throw, try, typeof, var, void, while, with, yield
Practical impact: If you have a TypeScript background and instinctively reach for let, const, or var, reach for const in Compact. If you reach for interface or class, use struct. If you reach for null, use none<T>() or default<T>().
Compiler-Reserved Prefix
Any identifier beginning with __compact is reserved for the compiler. Using one is a static error:
const __compact_internal = 1; // error: reserved prefix
Quick Lookup by Task
| Task | Keyword |
|---|---|
| Define a circuit | circuit |
| Make it callable from DApp | export circuit |
| Make a circuit side-effect free | pure circuit |
| Declare on-chain state | ledger |
| Make state readable from DApp | export ledger |
| Make state write-once | sealed ledger |
| Provide private input | witness |
| Initialize on deploy | constructor |
| Define a record type | struct |
| Define a sum type | enum |
| Create a distinct type | new type |
| Group code in a namespace | module |
| Bring in another module | import |
| Mark something public | export |
| Declare a version | pragma |
| Guard at runtime | assert |
| Cast a type | as |
| Declare disclosure | disclose |
| Default value | default |
| Transform a vector | map |
| Accumulate a vector | fold |
Common Mistakes
-
Using
typewhen you neednew type.type UserId = Bytes<32>is structural,Bytes<32>andUserIdare interchangeable. Usenew type UserId = Bytes<32>if you want them to be distinct. -
Using
letinstead ofconst. Compact has nolet. All local variables are declared withconstand are immutable after binding. -
Using
nullinstead ofnone<T>(). There’s nonullin Compact. Optional values useMaybe<T>:none<T>()orsome<T>(v). -
Using
classinstead ofstruct. Compact has noclass. Usestructfor record types. -
Forgetting
purewhen you want to document purity.pureis both documentation and a compiler-enforced assertion. If your circuit has no side effects, mark itpure, the compiler will catch if you accidentally add one later.
Cross-Links
- Previous: Compact Grammar Syntax rules
- Next: Testing and Debugging Errors and troubleshooting
- See also: Data Types Type system
- See also: Circuits Circuit syntax