Variables
Variables in Draft are declared with the @ prefix and are immutable by default.
Declaration
Section titled “Declaration”@name = "Draft"@version = 1@is_ready = trueType Inference
Section titled “Type Inference”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>Explicit Types
Section titled “Explicit Types”You can specify types explicitly:
@count: Int = 42@price: Float = 19.99@label: String = "Sale"@active: Boolean = true@items: Array<Int> = [1, 2, 3]Mutability
Section titled “Mutability”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 mutableDestructuring
Section titled “Destructuring”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} = @pointVariables 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 scopefin action.Shadowing
Section titled “Shadowing”Inner scopes can shadow outer variables:
@x = 10if true @x = 20 @// Shadows outer @x write(@x) @// Prints 20fin if.write(@x) @// Prints 10Constants
Section titled “Constants”Use const for compile-time constants:
const MAX_SIZE = 1000const PI = 3.14159const APP_NAME = "Draft"Constants are covered in detail in the Constants chapter.
Variable Naming Conventions
Section titled “Variable Naming Conventions”| 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
Section titled “Global Variables”Global variables are declared at the module level. Prefer module functions over globals:
@// Module-level variable@counter = 0
action increment() @counter = @counter + 1fin action.