init lux
This commit is contained in:
35
examples/effects.lux
Normal file
35
examples/effects.lux
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user