📌 Enumy
Enumy definiują zbiór nazwanych stałych.
Podstawowy enum
enum Color { RED, GREEN, BLUE }
print(Color.RED);
print(Color.GREEN);
print(Color.BLUE);
Enum z wartościami
enum Status {
PENDING = 1,
ACTIVE = 2,
DONE = 3
}
print(Status.PENDING);
print(Status.ACTIVE);
Użycie w warunkach
enum Priority { LOW, MEDIUM, HIGH }
fn processTask(task) {
if (task.priority == Priority.HIGH) {
print("Processing HIGH priority!");
}
}
let task = {name: "Important", priority: Priority.HIGH};
processTask(task);
Enum w when/switch
enum Direction { UP, DOWN, LEFT, RIGHT }
fn move(dir) {
let result = when dir {
Direction.UP -> "Moving up",
Direction.DOWN -> "Moving down",
Direction.LEFT -> "Moving left",
Direction.RIGHT -> "Moving right",
_ -> "Unknown direction"
};
return result;
}
print(move(Direction.UP));