Files
lux/examples/effects.lux
Brandon Lucas 15a820a467 fix: make all example programs work correctly
- Add string concatenation support to + operator in typechecker
- Register ADT constructors in both type environment and interpreter
- Bind handlers as values so they can be referenced in run...with
- Fix effect checking to use subset instead of exact match
- Add built-in effects (Console, Fail, State) to run block contexts
- Suppress dead code warnings in diagnostics, modules, parser

Update all example programs with:
- Expected output documented in comments
- Proper run...with statements to execute code

Add new example programs:
- behavioral.lux: pure, idempotent, deterministic, commutative functions
- pipelines.lux: pipe operator demonstrations
- statemachine.lux: ADT-based state machines
- tailcall.lux: tail call optimization examples
- traits.lux: type classes and pattern matching

Add documentation:
- docs/IMPLEMENTATION_PLAN.md: feature roadmap and status
- docs/PERFORMANCE_AND_TRADEOFFS.md: performance analysis

Add benchmarks for performance testing.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-13 09:05:06 -05:00

37 lines
877 B
Plaintext

// Demonstrating algebraic effects in Lux
//
// Expected output:
// [info] Processing data...
// [debug] Result computed
// Final result: 42
// Define a custom logging effect
effect Logger {
fn log(level: String, msg: String): Unit
fn getLevel(): String
}
// A function that uses the Logger effect
fn processData(data: Int): Int with {Logger} = {
Logger.log("info", "Processing data...")
let result = data * 2
Logger.log("debug", "Result computed")
result
}
// A handler that prints logs to console
handler consoleLogger: Logger {
fn log(level, msg) = Console.print("[" + level + "] " + msg)
fn getLevel() = "debug"
}
// Run and print
fn main(): Unit with {Console} = {
let result = run processData(21) with {
Logger = consoleLogger
}
Console.print("Final result: " + toString(result))
}
let output = run main() with {}