-
Notifications
You must be signed in to change notification settings - Fork 0
Operators
Here is a list of all operators in the language.
To note: when referring to "truthy" or "falsy", here is some examples: truthy: true, 1, "hello", (anything else not null)
false: null, 0, ""
Using arithmetic operators is simple, have a left value and a right value and place the operator inbetween.
+, -, *, / - What you expect them to do
% - modulus, divides the number and returns the remainder
** - power
Examples:
2 + 2 // 4
2 - 2 // 0
4 % 3 // 1
6 % 4 // 2
2 ** 2 // 4
2 ** 3 // 8
Comparison operators do as what their name suggest, compare two examples, for example:
2 == 2 // This is true
List of them:
==
- Basic compare, checks if the two values are the same
!=
- Checks if the two values are NOT the same
>
- For numbers, checks if the left is greater than the the right
<
- Checks if the left is less than the right
>=
- Checks if the left is greater than or equal to the right
<=
- Checks if the left is less than or equal to the right
<=>
- Multi compare:
- If left is greater, it returns 1
- If right is greater, it returns -1
- If they are the same, it returns 0
Examples:
1 == 1 // true
1 != 1 // false
9 > 4 // true
1 <=> 0 // 1
0 <=> 1 // -1
1 <=> 1 // 0
These work the exact same as arithmetic operators, though its quicker and shorter.
Quick examples:
var a = 2; // The assignment operator which simply sets the left to the right
a = a + 2; // Same as the one below
a += 2; // Same as the one above
There is also the coalesence assignment operator ??=
which checks sets the left to the right, if the left is null
var a = null;
a ??= 2; // a now equals 2
a = 3;
a ??= 4; // a is still 3
Logical operators turn both left and right into a truthy or falsy value, and then compares.
and
: Checks if both left AND right are truthy
- 1 and 2
or
: Checks if either left OR right OR both are truthy
- true or false
- true or true
!
: Not, converts the right to truthy if it is falsy and vice versa, this is a unary operator
- !true == false
- !1 == false
xor
: Exlusive not, checks if either left OR right but NOT both are truhy
- true xor false
- true xor true - bad
Binary operators are just operators which have a left and a right, comparison, some logical are all binary operators, but labelled differently.
??
- Coalesence operator, you have alrady seen this
>>
- Pipe operator, read the pipe page
->
- Casts a type to another type, on the left will be a value and on the right is a type, for example:
- 12 -> string (returns "12")
- 12 -> float (returns 12.0)
- 3.1 -> int (returns 3)
These are operators that work with only one value, either left or right, depending on the operator, this can change.
$
: Appears at the start of a value (array, string, enummerable), it gets the length of the value
- $"hello" (5)
- $[1, 2, 3] (3)
- $1..3 (3)
++
: Appears at the end of a variable name or number, increases it by one
- 1++ (2)
--
: Appears at the end of a variable name or number, decreases it by one
- 1-- (0)
Ternary operators are operators which have 3 differnet parts, in this language, there is only one time this happens.
Here is an example:
var a = 2;
var b = a == 2 ? true : false;
The first part, a == 2
, this is the test, if this is true than the expression after the ?
will be provided, in this case b will be true
If the test was not true, then the expression after the :
will be provided.
This works the exact same as:
var a = 2;
var b;
if a == 2 {
b = true;
else {
b = false;
}
The range operator creates an enumerable list of numbers based on a start, optional step and an end.
The basic syntax is as follows:
step..[step]..end;
[]
indicates it is optional.
Examples:
1..5; [1, 2, 3, 4, 5];
1..2..10; [1, 3, 5, 7, 9];
You can also have the ending be uninclusive:
1.<5; [1, 2, 3, 4];
1..2.<9; [1, 3, 4, 7];