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 = false

Immutability

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 constants

Use 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 = true

Naming 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

Featureconstlet
Type annotationRequiredOptional
ReassignmentNot allowedAllowed
Type inferenceNoYes
Use caseFixed valuesMutable values