Skip to content
Draft

Variables

Variables in Draft are declared with the @ prefix and are immutable by default.

@name = "Draft"
@version = 1
@is_ready = true

Draft infers types from the assigned value:

@count = 42 @// Int
@price = 19.99 @// Float
@label = "Sale" @// String
@active = true @// Boolean
@items = [1, 2, 3] @// Array<Int>

You can specify types explicitly:

@count: Int = 42
@price: Float = 19.99
@label: String = "Sale"
@active: Boolean = true
@items: Array<Int> = [1, 2, 3]

Variables are immutable (read-only) by default. Use mut to make them mutable:

@x = 5
@x = 10 @// Error: cannot reassign immutable variable
mut @y = 5
@y = 10 @// OK: y is mutable

Extract values from arrays, maps, or records:

@// Array destructuring
[@first, @second, @_rest] = [1, 2, 3, 4, 5]
@// Map destructuring
{@name: @userName, @age: @userAge} = get_user()
@// Record destructuring
{@x, @y} = @point

Variables are scoped to their block:

action example()
@outer = "outside"
if true
@inner = "inside"
write(@outer) @// OK: can access outer scope
fin if.
write(@inner) @// Error: @inner is out of scope
fin action.

Inner scopes can shadow outer variables:

@x = 10
if true
@x = 20 @// Shadows outer @x
write(@x) @// Prints 20
fin if.
write(@x) @// Prints 10

Use const for compile-time constants:

const MAX_SIZE = 1000
const PI = 3.14159
const APP_NAME = "Draft"

Constants are covered in detail in the Constants chapter.

Convention Use Case Example
snake_case Local variables @user_name
snake_case Module-level variables @config_path
CONSTANT_CASE Constants MAX_SIZE
_underscore Unused variables @_unused

Global variables are declared at the module level. Prefer module functions over globals:

@// Module-level variable
@counter = 0
action increment()
@counter = @counter + 1
fin action.

Continue to Constants and Types for more detail.