beginner
Variables and Values
Understanding Draft's variable system, types, and mutability.
Variables and Values
Draft uses an explicit variable declaration system with @ prefixed identifiers. This tutorial covers variable assignment, type inference, and mutability.
Declaring Variables
Variables in Draft are declared with the @ prefix and assigned with =.
action main() @name = "Draft" @version = 0.1 @count = 42fin action.Draft infers the type of each variable from its initial value. You do not need to write type annotations unless you want to.
Types
Draft has a small set of built-in types:
| Type | Example | Description |
|---|---|---|
int |
42 |
Signed integer |
float |
3.14 |
Floating-point number |
str |
"hello" |
UTF-8 string |
bool |
true |
Boolean |
list |
[1, 2, 3] |
Ordered collection |
map |
{"a": 1} |
Key-value mapping |
void |
null |
Empty value |
Type Inference
The compiler infers types from literals and expressions:
action main() @x = 10 // int @y = 3.14 // float @greeting = "hi" // str @active = true // boolfin action.You can also write explicit types when needed:
action main() @score: int = 0 @ratio: float = 1.5 @name: str = "player"fin action.Mutability
Variables are immutable by default. Use mut to declare a mutable variable.
action main() @score = 0 // immutable @mut level = 1 // mutable
level = level + 1 // allowed // score = 10 // error: score is immutablefin action.Immutable variables help catch bugs at compile time and allow the compiler to optimize more aggressively.
Member Access
Access fields and methods with #:
action main() @user = {"name": "Ada", "score": 100} write(user#name) // prints: Ada write(user#score) // prints: 100fin action.Strings and Interpolation
Concatenate strings with + or use interpolation:
action main() @name = "Draft" @message = "Hello, " + name + "!" write(message) // prints: Hello, Draft!fin action.Lists
Create lists with square brackets:
action main() @numbers = [1, 2, 3, 4, 5] @first = numbers[0] @last = numbers[-1] write(first) // prints: 1 write(last) // prints: 5fin action.Best Practices
- Prefer immutable variables unless mutation is required
- Let the compiler infer types in simple cases
- Use descriptive variable names
Next Steps
- Build a CLI tool that accepts user input
- Learn about HTTP servers
