Pragma and Imports
Pragma Statement
The pragma statement declares which Compact language versions your code supports.
Syntax
// pragma language_version >= 0.16 && <= 0.23;
This means:
- Minimum version: 0.16
- Maximum version: 0.23
Why Version Constraints?
Compact is evolving. New versions may:
- Add new features
- Change existing syntax
- Deprecate old patterns
By specifying version bounds, you ensure:
- Your code compiles with compatible compilers
- Future breaking changes don’t silently break your contract
- Users know which compiler version to use
If the Compact compiler does not support the language versions specified, it will signal a compile-time error.
Version Constraint Options
// Exact version match (version number only)
pragma language_version 0.23;
// Minimum version (open-ended)
pragma language_version >= 0.16;
// Version range (recommended)
pragma language_version >= 0.16 && <= 0.23;
Tip: Your installed compiler implements exactly one language version — check it with
compact compile --language-version(currently0.23.0with toolchain 0.31.1). A pragma whose range doesn’t include that version fails with alanguage version ... mismatcherror, so keep the upper bound at or above your compiler’s language version.
Best Practices
Note: These are recommended conventions, not official rules from the Midnight documentation.
// ✅ Recommended - specific version range
pragma language_version >= 0.16 && <= 0.23;
// ✅ Exact version - for strict compatibility
pragma language_version 0.23;
// ⚠️ Caution - open-ended (may break with future versions)
pragma language_version >= 0.16;
Note: Compact doesn’t support
==for version comparison. For an exact version match, simply write the version number without any comparison operator:pragma language_version 0.23;
Import Statement
The import statement brings external modules into scope.
Standard Library
Every Compact program should import the standard library:
import CompactStandardLibrary;
This provides ledger ADTs and useful helper types and functions.
Import Syntax
// Import entire module
import CompactStandardLibrary;
// Import from a path (only compiles if the file exists in your project)
import "path/to/module";
The import mechanism supports:
import M;— import a moduleimport "path/to/M";— import from a path- Optional prefix and type parameters
Module Contents
After importing CompactStandardLibrary, you have access to:
| Category | Items |
|---|---|
| Ledger ADTs | Counter, Map<K,V>, MerkleTree<N,T> |
| Optional Types | Maybe<T>, Either<L,R>, JubjubPoint, MerkleTreeDigest, etc. |
| Hash Functions | persistentHash<T>(), persistentCommit<T>(), transientHash<T>(), transientCommit<T>() |
| Helpers | some<T>(), none<T>(), left<L,R>(), right<L,R>(), pad() |
Note: The standard library also includes EC functions, Merkle functions, and coin-management functions. See the Compact Standard Library Reference for the complete list.
Complete Template
Here’s a minimal complete Compact file (matches the official counter example pattern):
pragma language_version >= 0.16;
import CompactStandardLibrary;
export ledger value: Counter;
export circuit increment(): [] {
value.increment(1);
}
Exercises
Practice pragma and import statements in exercises/01_basics/02_pragma_imports/.