This commit is contained in:
2026-02-13 02:57:01 -05:00
commit 15e5ccb064
23 changed files with 11899 additions and 0 deletions

35
examples/effects.lux Normal file
View File

@@ -0,0 +1,35 @@
// Demonstrating algebraic effects in Lux
// 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"
}
// A handler that ignores logs (for testing)
handler nullLogger: Logger {
fn log(level, msg) = ()
fn getLevel() = "none"
}
// Main function showing handler usage
fn main(): Unit with {Console} = {
let result = run processData(21) with {
Logger = consoleLogger
}
Console.print("Final result: " + result)
}