馃幆 Typy danych
KitsuneScript jest j臋zykiem dynamicznie typowanym. Typ zmiennej jest okre艣lany w runtime i mo偶e si臋 zmienia膰.
Podstawowe typy
| Typ |
Opis |
Przyk艂ad |
number |
Liczby (ca艂kowite i zmiennoprzecinkowe) |
42, 3.14, -5 |
string |
Tekst |
"hello", 'world' |
bool |
Warto艣膰 logiczna |
true, false |
null |
Brak warto艣ci |
null |
list |
Tablica warto艣ci |
[1, 2, 3] |
map |
S艂ownik klucz-warto艣膰 |
{name: "Kit"} |
function |
Funkcja |
fn(x) { x * 2 } |
Liczby (number)
let int = 42;
let negative = -10;
let float = 3.14;
let scientific = 1.5e10;
let million = 1_000_000;
let bigNum = 1_234_567_890;
let binary = 0b1010;
let flags = 0b1111_0000;
let hex = 0xFF;
let color = 0xFFAABB;
Stringi (string)
let str1 = "Hello, World!";
let str2 = 'Single quotes';
let name = "Kitsune";
let msg = `Cze艣膰, jestem ${name}!`;
let a = 10, b = 20;
print(`${a} + ${b} = ${a + b}`);
let escaped = "Line 1\nLine 2\tTabbed";
let path = r"C:\Users\Name\Documents";
let regex = r"\d+\.\d+";
let poem = """
Roses are red,
Violets are blue,
KitsuneScript is great!
""";
Booleany (bool)
let yes = true;
let no = false;
let and = true && false;
let or = true || false;
let not = !true;
if (1) print("truthy");
if ("text") print("truthy");
Null
let nothing = null;
if (nothing == null) {
print("To jest null");
}
let value = null;
let safe = value ?? "default";
let obj = null;
let prop = obj?.name;
Listy (list)
let numbers = [1, 2, 3, 4, 5];
let mixed = [1, "text", true, null];
let empty = [];
print(numbers[0]);
print(numbers[-1]);
print(numbers[-2]);
numbers[0] = 100;
print(numbers);
print(size(numbers));
print(numbers.length);
let list = [1, 2, 3,];
Mapy (map)
let person = {
name: "Kitsune",
age: 25,
active: true
};
print(person.name);
print(person["age"]);
person.age = 26;
person["email"] = "kit@example.com";
let key = "dynamicKey";
let obj = {
[key]: 42,
["prefix_" + key]: 100
};
print(obj["dynamicKey"]);
let x = 10, y = 20;
let point = {x, y};
let counter = {
value: 0,
increment: fn() {
this.value = this.value + 1;
return this.value;
}
};
print(counter.increment());
Sprawdzanie typ贸w
print(typeOf(42));
print(typeOf("hello"));
print(typeOf(true));
print(typeOf(null));
print(typeOf([1,2,3]));
print(typeOf({a:1}));
print(isNumber(42));
print(isString("text"));
print(isBool(true));
print(isNull(null));
print(isList([1,2]));
print(isMap({a:1}));
print(isFunction(fn(){}));
if (value is string) {
print("To jest string!");
}
Konwersja typ贸w
print(toString(42));
print(toString([1,2,3]));
print(toNumber("42"));
print(toNumber(true));
print(toNumber(false));
print(toInt(3.7));
print(toList("abc"));
print(toMap([["a",1],["b",2]]));
let num = "42" as! number;
let maybeNum = "abc" as? number;
馃摑 Uwaga
KitsuneScript u偶ywa as! dla force cast i as? dla safe cast. Samo as jest zarezerwowane dla innych konstrukcji.