Files
lux/examples/traits.lux
2026-02-17 06:52:44 -05:00

36 lines
748 B
Plaintext

trait Printable {
fn format(value: Int): String
}
impl Printable for Int {
fn format(value: Int): String = "Number: " + toString(value)
}
type Color =
| Red
| Green
| Blue
| RGB(Int, Int, Int)
fn colorName(c: Color): String =
match c {
Red => "red",
Green => "green",
Blue => "blue",
RGB(r, g, b) => "rgb(" + toString(r) + "," + toString(g) + "," + toString(b) + ")",
}
let myColor = RGB(255, 128, 0)
let redColor = Red
let greenColor = Green
fn printResults(): Unit with {Console} = {
Console.print("RGB color: " + colorName(myColor))
Console.print("Red color: " + colorName(redColor))
Console.print("Green color: " + colorName(greenColor))
}
let output = run printResults() with {}