馃摜 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 "math_utils.ks" in math;
import "string_utils.ks" in str;
log(math.factorial(5));
log(math.isPrime(17));
log(str.capitalize("hello"));
Eksportowanie
U偶yj export przed deklaracj膮, aby udost臋pni膰 j膮 innym modu艂om.
Export zmiennych
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
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
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
| Cecha | use "module" | import "file.ks" in alias |
| 殴r贸d艂o | Wbudowane modu艂y stdlib | Pliki .ks |
| Scope | Globalny (bez prefiksu) | Przez alias |
| Przyk艂ad u偶ycia | sqrt(16) | math.sqrt(16) |
| Wielokrotne | use "math", "string"; | Osobne instrukcje |
use "math";
log(sqrt(16));
import "my_math.ks" in myMath;
log(myMath.square(5));