馃摜 Import i Export modu艂贸w

KitsuneScript pozwala na tworzenie w艂asnych modu艂贸w i dzielenie kodu mi臋dzy plikami.

Importowanie modu艂贸w

Sk艂adnia: import "艣cie偶ka/do/pliku.ks" in alias;

// Import modu艂u do aliasu import "math_utils.ks" in math; import "string_utils.ks" in str; // U偶ycie funkcji przez alias log(math.factorial(5)); // 120 log(math.isPrime(17)); // true log(str.capitalize("hello")); // "Hello"

Eksportowanie

U偶yj export przed deklaracj膮, aby udost臋pni膰 j膮 innym modu艂om.

Export zmiennych

// my_module.ks export let VERSION = "1.0.0"; export let PI = 3.14159; export const MAX_SIZE = 100;

Export funkcji

export fn square(x) { return x * x; } export fn cube(x) { return x * x * x; } export fn circleArea(radius) { return PI * radius * radius; }

Export klas

export class Vector { fn init(x, y) { this.x = x; this.y = y; } fn add(other) { return Vector(this.x + other.x, this.y + other.y); } fn magnitude() { return sqrt(this.x * this.x + this.y * this.y); } }

Przyk艂ad kompletnego modu艂u

// math_utils.ks export let VERSION = "1.0.0"; export fn factorial(n) { if (n <= 1) return 1; return n * factorial(n - 1); } export fn fibonacci(n) { if (n <= 1) return n; return fibonacci(n - 1) + fibonacci(n - 2); } export fn isPrime(n) { if (n < 2) return false; let i = 2; while (i * i <= n) { if (n % i == 0) return false; i = i + 1; } return true; }

U偶ycie modu艂u

// main.ks import "math_utils.ks" in math; log("Wersja: " + math.VERSION); log("5! = " + math.factorial(5)); log("fib(10) = " + math.fibonacci(10)); log("isPrime(17) = " + math.isPrime(17));

R贸偶nica: use vs import

Cechause "module"import "file.ks" in alias
殴r贸d艂oWbudowane modu艂y stdlibPliki .ks
ScopeGlobalny (bez prefiksu)Przez alias
Przyk艂ad u偶yciasqrt(16)math.sqrt(16)
Wielokrotneuse "math", "string";Osobne instrukcje
// use - wbudowane modu艂y use "math"; log(sqrt(16)); // 4 - bezpo艣rednio // import - w艂asne pliki import "my_math.ks" in myMath; log(myMath.square(5)); // przez alias