Skip to content

Best Practices

Code Style

wyn
// var for mutable, const for immutable
var count = 0
const MAX_SIZE = 1024

// Functions: verb_noun naming
fn calculate_total(items: [int]) -> int { ... }

// Structs: PascalCase
struct HttpRequest { method: string, path: string }

Error Handling

Use ResultInt/ResultString with unwrap_or:

wyn
fn load_config(path: string) -> ResultInt {
    if File.exists(path) == 0 { return Err("not found: " + path) }
    var content = File.read(path)
    return Ok(content.len())
}

Concurrency

Prefer spawn/await for parallel work. Use Task.value for shared state:

wyn
var f1 = spawn process(chunk1)
var f2 = spawn process(chunk2)
var r1 = await f1
var r2 = await f2

Use channels for producer/consumer:

wyn
var ch = Task.channel(100)
spawn producer(ch)
var val = Task.recv(ch)

Collections

Use functional methods for data pipelines:

wyn
var result = data
    .filter(fn(x: int) -> int { return x > 0 })
    .map(fn(x: int) -> int { return x * 2 })
    .reduce(fn(a: int, b: int) -> int { return a + b }, 0)

File I/O

wyn
// Small files: read all at once
var content = File.read("small.txt")

// Large files: stream line by line
var fh = File.open("large.csv", "r")
while File.eof(fh) == 0 {
    var line = File.read_line(fh).trim()
}
File.close(fh)

Testing

wyn
fn main() -> int {
    Test.init("My Module")
    Test.assert_eq_int(add(2, 3), 5, "basic add")
    Test.assert_eq_str(name, "expected", "name matches")
    Test.summary()
    return 0
}

MIT License