Constants
Constants require an explicit type and cannot be reassigned after initialization.
Constant Declaration
kode
const MAX: int = 100
const PI: float = 3.14159
const APP_NAME: string = "MyApp"
const DEBUG: bool = falseImmutability
Once a constant is defined, attempting to reassign it will result in a compile-time error:
kode
const MAX: int = 100
MAX = 200 // ERROR: Cannot reassign constantsUse Cases
Constants are ideal for:
- Configuration values
- Mathematical constants
- Fixed application settings
- Version numbers
kode
const VERSION: string = "0.3.1"
const MAX_RETRIES: int = 3
const TIMEOUT: float = 30.0
const ENABLED: bool = trueNaming Convention
By convention, constants use UPPER_SNAKE_CASE:
kode
const MAX_CONNECTIONS: int = 100
const DEFAULT_TIMEOUT: float = 30.0
const APP_VERSION: string = "1.0.0"Constants vs Variables
| Feature | const | let |
|---|---|---|
| Type annotation | Required | Optional |
| Reassignment | Not allowed | Allowed |
| Type inference | No | Yes |
| Use case | Fixed values | Mutable values |