Skip to content
Draft

Constants

Constants are immutable values known at compile time.

const MAX_CONNECTIONS = 1000
const APP_VERSION = "1.4.0"
const DEBUG_MODE = false

Constants can be assigned from constant expressions:

const BASE_URL = "https://api.example.com"
const API_PATH = "/v1"
const FULL_URL = BASE_URL + API_PATH

Type annotations are optional but recommended for clarity:

const TIMEOUT: Int = 30
const MAX_RETRIES: Int = 3
const DEFAULT_NAME: String = "anonymous"

Constants are typically declared at the module level:

const PI = 3.14159265358979
const E = 2.71828182845905
action circle_area(@radius: Float) -> Float
return PI * @radius * @radius
fin action.

Constants can be defined inside actions:

action process()
const LOCAL_LIMIT = 50
for @i in 0..LOCAL_LIMIT
process_item(@i)
fin for.
fin action.

Enums define a set of named constants:

enum Color
Red
Green
Blue
fin enum.
@// Usage
@primary = Color#Red

See Enums for full details.

Feature Variable (@) Constant (const)
Mutability Mutable with mut Always immutable
Assignment Runtime Compile time
Scope Block or module Block or module
Type inference Yes Yes
Shadowing Yes No

Constants use CONSTANT_CASE (uppercase with underscores):

const MAX_BUFFER_SIZE = 8192
const DEFAULT_TIMEOUT_MS = 5000
const ENABLE_LOGGING = true

Constants can be imported from other modules:

import { MAX_RETRIES, API_KEY } from config
action connect()
for @i in 0..MAX_RETRIES
if try_connect()
return true
fin if.
fin for.
return false
fin action.

Constants are evaluated at compile time, enabling optimizations:

const SIZE = 1024 * 1024 @// Computed at compile time
const FLAGS = 0x01 | 0x02 @// Bitwise operations allowed

Expressions in constants must be determinable at compile time. Function calls are not allowed unless they are const functions (future feature).

Learn about Types to understand Draft’s type system.