🔧 Funkcje

Funkcje to podstawowe bloki budulcowe kodu w KitsuneScript.

Deklaracja funkcji

// Podstawowa funkcja fn greet(name) { return "Hello, " + name + "!"; } print(greet("Kitsune")); // "Hello, Kitsune!" // Funkcja bez return zwraca null fn sayHi() { print("Hi!"); }

Parametry domyślne

fn greet(name = "World", greeting = "Hello") { return `${greeting}, ${name}!`; } print(greet()); // "Hello, World!" print(greet("Kit")); // "Hello, Kit!" print(greet("Kit", "Cześć")); // "Cześć, Kit!"

Rest parameters (...)

fn sum(...numbers) { let total = 0; for (n in numbers) { total = total + n; } return total; } print(sum(1, 2, 3)); // 6 print(sum(1, 2, 3, 4, 5)); // 15 // Kombinacja z normalnymi parametrami fn log(prefix = ">", ...items) { for (item in items) { print(prefix + " " + item); } }

Funkcje anonimowe (lambda)

// Przypisanie do zmiennej let multiply = fn(a, b) { return a * b; }; print(multiply(4, 5)); // 20 // Jako argument 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); // OK: 5 divide(10, 0); // Error: Guard clause failed // Warunek może używać wielu parametrów 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; } // Stwórz funkcję z wypełnionym argumentem let add5 = add(5, _, _); print(add5(10, 20)); // 35 fn greet(greeting, name, punctuation) { return greeting + ", " + name + punctuation; } let sayHello = greet("Hello", _, "!"); print(sayHello("World")); // "Hello, World!"

Rekurencja

fn factorial(n) { if (n <= 1) return 1; return n * factorial(n - 1); } print(factorial(5)); // 120 fn fibonacci(n) { if (n <= 1) return n; return fibonacci(n - 1) + fibonacci(n - 2); }

Funkcje jako wartości

// Funkcje mogą być przekazywane jako argumenty fn apply(fn, value) { return fn(value); } fn double(x) { return x * 2; } fn square(x) { return x * x; } print(apply(double, 5)); // 10 print(apply(square, 5)); // 25 // Funkcje mogą być zwracane fn multiplier(factor) { return fn(x) { return x * factor; }; } let triple = multiplier(3); print(triple(10)); // 30

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 + "!"; } // Nazwa funkcji w zmiennej let func = "sayHello"; print(func("Kitsune")); // Hello, Kitsune! // Zmiana w runtime func = "sayBye"; print(func("Kitsune")); // Bye, Kitsune!
// Dynamiczny dispatch z mapy 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)); // 30 op = ops.times; print(op(10, 20)); // 200
// Wywoływanie w pętli let funcs = ["sayHello", "sayBye"]; for (f in funcs) { print(f("Kit")); } // Hello, Kit! // Bye, 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; // Widzi zmienne z bieżącego scope let result = eval("x + y"); print(result); // 30 // Może definiować nowe zmienne eval("let z = x * y"); print(z); // 200 // Może definiować funkcje eval("fn square(n) { return n * n; }"); print(square(7)); // 49
// Dynamiczne generowanie kodu let op = "+"; let code = "x " + op + " y"; print(eval(code)); // 30 // Wywołanie istniejących funkcji fn greet(name) { return "Hello, " + name; } print(eval("greet(\"World\")")); // Hello, 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.