Skip to content

Control Flow & Patterns

Flame provides a robust set of control flow structures designed for both safety and readability.


Standard conditional branching:

let score = 85
if score >= 90 {
print("Grade: A")
} else if score >= 80 {
print("Grade: B")
} else {
print("Grade: C")
}

Flame features powerful, expressive pattern matching with match:

let status_code = 200
match status_code {
200 => print("OK"),
404 => print("Not Found"),
500 => print("Internal Server Error"),
_ => print("Unknown Status Code") // `_` acts as default fallback
}

You can unpack data embedded inside Enums (like Result and Option) using tuple destructuring (value) and paths in your match arms. If your logic requires multiple statements, you can use { ... } blocks directly as the body of your match arm!

let opt = Option.Some(42)
match opt {
Option.Some(value) => {
let doubled = value * 2
print($"Found value: {doubled}")
},
Option.None => print("No value found!")
}
let result: Result<Int, Error> = divide(10, 2)
match result {
Result.Ok(ans) => print($"Success: {ans}"),
Result.Err(err) => {
print("An error occurred!")
print(err.message)
}
}

Flame supports three distinct looping paradigms:

Executes as long as the condition evaluates to true:

let mut count = 0
while count < 5 {
print($"Count: {count}")
count = count + 1
}

Creates an infinite loop, ideal for servers, game loops, or event dispatchers:

let mut ticks = 0
loop {
ticks = ticks + 1
if ticks > 10 {
break // explicitly break out
}
}

Iterates over dynamic collections, ranges, or vectors:

let fruits = ["Apple", "Banana", "Orange"]
for fruit in fruits {
print($"Fruit: {fruit}")
}

The defer statement schedules a statement or block of code to run at the exact moment the enclosing function or scope exits.

import std.fs
fn process_log() {
let file = fs.read("app.log")
defer print("Finished processing file!") // Runs when process_log() returns
// ... parse log data ...
if file.len() == 0 {
return // defer still runs here!
}
print("Log processed successfully.")
}

  • break: Immediately terminates the innermost loop.
  • continue: Skips the remaining statements in the current iteration and begins the next iteration.
  • return: Exits the current function, optionally returning a value.