🔧 Funkcje
Funkcje to podstawowe bloki budulcowe kodu w KitsuneScript.
Deklaracja funkcji
fn greet(name) {
return "Hello, " + name + "!";
}
print(greet("Kitsune"));
fn sayHi() {
print("Hi!");
}
Parametry domyślne
fn greet(name = "World", greeting = "Hello") {
return `${greeting}, ${name}!`;
}
print(greet());
print(greet("Kit"));
print(greet("Kit", "Cześć"));
Rest parameters (...)
fn sum(...numbers) {
let total = 0;
for (n in numbers) {
total = total + n;
}
return total;
}
print(sum(1, 2, 3));
print(sum(1, 2, 3, 4, 5));
fn log(prefix = ">", ...items) {
for (item in items) {
print(prefix + " " + item);
}
}
Funkcje anonimowe (lambda)
let multiply = fn(a, b) {
return a * b;
};
print(multiply(4, 5));
let numbers = [1, 2, 3];
let doubled = numbers.map(fn(x) { return x * 2; });
Guard clauses (where)
Warunki wstępne dla funkcji:
fn divide(a, b) where b != 0 {
return a / b;
}
divide(10, 2);
divide(10, 0);
fn createUser(name, age) where size(name) > 0 && age >= 0 {
return {name: name, age: age};
}
Partial Application
Użyj _ jako placeholder:
fn add(a, b, c) {
return a + b + c;
}
let add5 = add(5, _, _);
print(add5(10, 20));
fn greet(greeting, name, punctuation) {
return greeting + ", " + name + punctuation;
}
let sayHello = greet("Hello", _, "!");
print(sayHello("World"));
Rekurencja
fn factorial(n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
print(factorial(5));
fn fibonacci(n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
Funkcje jako wartości
fn apply(fn, value) {
return fn(value);
}
fn double(x) { return x * 2; }
fn square(x) { return x * x; }
print(apply(double, 5));
print(apply(square, 5));
fn multiplier(factor) {
return fn(x) {
return x * factor;
};
}
let triple = multiplier(3);
print(triple(10));
Dynamiczne wywoływanie (jak PHP)
Nazwa funkcji może być przechowywana jako string i wywoływana dynamicznie - podobnie jak $func() w PHP:
fn sayHello(name) {
return "Hello, " + name + "!";
}
fn sayBye(name) {
return "Bye, " + name + "!";
}
let func = "sayHello";
print(func("Kitsune"));
func = "sayBye";
print(func("Kitsune"));
fn add(a, b) { return a + b; }
fn mul(a, b) { return a * b; }
let ops = {
plus: "add",
times: "mul"
};
let op = ops.plus;
print(op(10, 20));
op = ops.times;
print(op(10, 20));
let funcs = ["sayHello", "sayBye"];
for (f in funcs) {
print(f("Kit"));
}
💡 Uwaga
String musi odpowiadać nazwie istniejącej funkcji w bieżącym scope lub zarejestrowanej host function. Jeśli funkcja nie istnieje, zostanie rzucony błąd Cannot call 'name': function not found.
eval() - Dynamiczne wykonanie kodu
Funkcja eval() parsuje i wykonuje string z kodem KitsuneScript w bieżącym kontekście. Ma pełny dostęp do zmiennych, funkcji, host functions i modułów:
let x = 10;
let y = 20;
let result = eval("x + y");
print(result);
eval("let z = x * y");
print(z);
eval("fn square(n) { return n * n; }");
print(square(7));
let op = "+";
let code = "x " + op + " y";
print(eval(code));
fn greet(name) {
return "Hello, " + name;
}
print(eval("greet(\"World\")"));
⚠️ Bezpieczeństwo
eval() wykonuje dowolny kod - nigdy nie przekazuj niezaufanych danych do eval(). Używaj go do dynamicznego generowania kodu, konfiguracji, lub prostych wyrażeń. Błędy składniowe w eval() są łapane i rzucane jako wyjątek runtime.