⚡ Operatory
KitsuneScript oferuje bogaty zestaw operatorów, w tym wiele nowoczesnych konstrukcji znanych z innych języków.
Operatory arytmetyczne
| Operator |
Opis |
Przykład |
Wynik |
+ | Dodawanie | 10 + 5 | 15 |
- | Odejmowanie | 10 - 5 | 5 |
* | Mnożenie | 10 * 5 | 50 |
/ | Dzielenie | 10 / 5 | 2 |
% | Modulo | 10 % 3 | 1 |
** | Potęgowanie | 2 ** 3 | 8 |
Operatory porównania
| Operator |
Opis |
Przykład |
== | Równość | 5 == 5 → true |
!= | Nierówność | 5 != 3 → true |
< | Mniejsze | 3 < 5 → true |
> | Większe | 5 > 3 → true |
<= | Mniejsze lub równe | 3 <= 3 → true |
>= | Większe lub równe | 5 >= 5 → true |
<=> | Spaceship (trójstronny) | 5 <=> 10 → -1 |
Łańcuchowe porównania
let x = 5;
print(1 < x < 10);
print(10 < x < 20);
print(5 <=> 10);
print(10 <=> 5);
print(5 <=> 5);
Operatory logiczne
| Operator |
Opis |
Przykład |
&& | Logiczne AND | true && false → false |
|| | Logiczne OR | true || false → true |
! | Logiczne NOT | !true → false |
not | Słowne NOT | not true → false |
Operatory bitowe
let and = 12 & 10;
let or = 12 | 10;
let xor = 12 ^ 10;
let not = ~5;
let shl = 1 << 4;
let shr = 16 >> 2;
let READ = 1;
let WRITE = 2;
let EXECUTE = 4;
let permissions = READ | WRITE;
let hasRead = (permissions & READ) != 0;
Operatory null
let value = null;
let safe = value ?? "default";
let zero = 0;
let result = zero ?: "default";
let obj = {name: "Kit"};
print(obj?.name);
print(obj?.missing);
let list = [1, 2, 3];
print(list?[0]);
print(list?[100]);
let nullList = null;
print(nullList?[0]);
Operatory specjalne
Ternary operator (? :)
let result = (x > 0) ? "positive" : "non-positive";
Range operators (.., ..<)
let nums = 1..5;
let reverse = 5..1;
let indices = 0..<5;
let odds = 1..20 step 2;
let countdown = 10..0 step -2;
In / not in operators
let list = [1, 2, 3];
print(2 in list);
print(10 in list);
print(5 not in list);
print("lo" in "hello");
let obj = {name: "Kit"};
print("name" in obj);
Spread operator (...)
let a = [1, 2, 3];
let b = [...a, 4, 5];
let obj1 = {x: 1};
let obj2 = {...obj1, y: 2};
Pipe operator (|>)
fn double(x) { return x * 2; }
fn addTen(x) { return x + 10; }
let result = 5 |> double |> addTen;
String/List multiplication
let stars = "*" * 5;
let border = "=" * 20;
let laugh = 3 * "ha";
let pattern = [1, 2] * 3;
let zeros = [0] * 5;
Composition operator (>>)
fn double(x) { return x * 2; }
fn addOne(x) { return x + 1; }
let doubleThenAdd = double >> addOne;
print(doubleThenAdd(5));
Operatory przypisania
| Operator |
Równoważne |
Opis |
+= | x = x + y | Dodaj i przypisz |
-= | x = x - y | Odejmij i przypisz |
*= | x = x * y | Pomnóż i przypisz |
/= | x = x / y | Podziel i przypisz |
%= | x = x % y | Modulo i przypisz |
??= | if null: x = y | Przypisz gdy null |
||= | if falsy: x = y | Przypisz gdy falsy |
&&= | if truthy: x = y | Przypisz gdy truthy |
Priorytet operatorów
Od najwyższego do najniższego:
| Priorytet |
Operatory |
| 1 (najwyższy) | () [] . ?. ?[] |
| 2 | ! ~ ++ -- (unary) |
| 3 | ** |
| 4 | * / % |
| 5 | + - |
| 6 | << >> |
| 7 | .. ..< |
| 8 | < <= > >= in |
| 9 | == != is |
| 10 | & |
| 11 | ^ |
| 12 | | |
| 13 | && |
| 14 | || |
| 15 | ?? ?: |
| 16 | ? : (ternary) |
| 17 | |> >> |
| 18 (najniższy) | = += -= itd. |