Control Flow & Patterns
Flame provides a robust set of control flow structures designed for both safety and readability.
Conditionals
Section titled “Conditionals”if / else if / else
Section titled “if / else if / else”Standard conditional branching:
let score = 85
if score >= 90 { print("Grade: A")} else if score >= 80 { print("Grade: B")} else { print("Grade: C")}Pattern Matching (match)
Section titled “Pattern Matching (match)”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}Enum Destructuring & Blocks
Section titled “Enum Destructuring & Blocks”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:
while Loops
Section titled “while Loops”Executes as long as the condition evaluates to true:
let mut count = 0while count < 5 { print($"Count: {count}") count = count + 1}Infinite Loops (loop)
Section titled “Infinite Loops (loop)”Creates an infinite loop, ideal for servers, game loops, or event dispatchers:
let mut ticks = 0loop { ticks = ticks + 1 if ticks > 10 { break // explicitly break out }}for .. in Iteration
Section titled “for .. in Iteration”Iterates over dynamic collections, ranges, or vectors:
let fruits = ["Apple", "Banana", "Orange"]
for fruit in fruits { print($"Fruit: {fruit}")}Scope Exit Cleanup (defer)
Section titled “Scope Exit Cleanup (defer)”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.")}Loop Modifiers
Section titled “Loop Modifiers”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.
