Standard examples (examples/standard/): - hello_world: Basic effect usage - fizzbuzz: Classic programming exercise - factorial: Recursive and tail-recursive versions - primes: Prime number generation - guessing_game: Interactive Random + Console effects - stdlib_demo: Demonstrates List, String, Option, Math modules Showcase examples (examples/showcase/): - ask_pattern: Resumable effects for config/environment - custom_logging: Custom effect with handler - early_return: Fail effect for clean error handling - effect_composition: Combining multiple effects - higher_order: Closures and function composition - pattern_matching: ADTs and exhaustive matching Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
40 lines
1.0 KiB
Plaintext
40 lines
1.0 KiB
Plaintext
// Early Return with Fail Effect
|
|
//
|
|
// The Fail effect provides clean early termination.
|
|
// Functions declare their failure modes in the type signature.
|
|
//
|
|
// Expected output:
|
|
// Parsing "42"...
|
|
// Result: 42
|
|
// Parsing "100"...
|
|
// Result: 100
|
|
// Dividing 100 by 4...
|
|
// Result: 25
|
|
|
|
fn parsePositive(s: String): Int with {Fail, Console} = {
|
|
Console.print("Parsing \"" + s + "\"...")
|
|
if s == "42" then 42
|
|
else if s == "100" then 100
|
|
else Fail.fail("Invalid number: " + s)
|
|
}
|
|
|
|
fn safeDivide(a: Int, b: Int): Int with {Fail, Console} = {
|
|
Console.print("Dividing " + toString(a) + " by " + toString(b) + "...")
|
|
if b == 0 then Fail.fail("Division by zero")
|
|
else a / b
|
|
}
|
|
|
|
fn main(): Unit with {Console} = {
|
|
// These succeed
|
|
let n1 = run parsePositive("42") with {}
|
|
Console.print("Result: " + toString(n1))
|
|
|
|
let n2 = run parsePositive("100") with {}
|
|
Console.print("Result: " + toString(n2))
|
|
|
|
let n3 = run safeDivide(100, 4) with {}
|
|
Console.print("Result: " + toString(n3))
|
|
}
|
|
|
|
let output = run main() with {}
|