-
Notifications
You must be signed in to change notification settings - Fork 0
Operators
Operators let you do specific operations on a pair of values (or a single value) and result in a new value. By now (if you read everything in order) you've seen three operators: =, .., and []. Let's go through each category.
Before we start, we need to talk about something called expressions. Every value itself and the results of operators are expressions, so:
-
[1, 2, 3]- this is array literal expression. -
1.0- this is real literal expression. -
2 + 2- this is binary term expression. -
f()- this is call expression.
This works with any value type, including functions and objects (we haven't talked about them yet but remember this part) too.
As you've read in Variables, this operator modifies the variable.
var a = 10;
a = 20; // a is now 20Concatenation means combining separate pieces of text into one. In Wi, concatenation supports all value types.
var a = 10;
"a" .. "b" .. a .. true .. null; // ab10truenull
true .. null; // truenull10 + 5; // 15 (addition)
10 - 5; // 5 (subtraction)
10 * 5; // 50 (multiplication)
10 / 5; // 2 (division)
10 % 3; // 1 (modulo - calculates the remainder of the division)
2 ** 3; // 8 (power)
// Comparison
10 == 10; // true (is equal to)
10 != 10; // false (is NOT equal to)
1 > 2; // false (greater than)
2 >= 2; // true (greater than OR equal to)
1 < 2; // false (less than)
2 <= 2; // true (less than OR equal to)&& means logical AND - the result is truthy if and only if both operands are truthy.
true && false; // false
true && true; // true
false && true; // false
false && false; // false|| means logical OR - the result is truthy if and only if at least one of operands is truthy.
true || false; // true
true || true; // true
false || true; // true
false || false; // false! means logical NOT - the result is the opposite of the operand. If operand is truthy, result is falsy, if operand is falsy, result is truthy.
!true; // false
!false; // true
!0; // true
!1; // falseThese operators work on comparison expressions because comparison results in a boolean value:
!(10 == 10); // false
!(10 != 10); // true
(1 > 2) && (2 >= 2); // false
(1 < 2) || (2 <= 2); // trueIt is worth mentioning that in Wi you can check falsiness of every value type. null, 0 and false is falsy - everything else is truthy.
Bitwise operators work on the binary representation of numbers. They operate on the individual bits (0s and 1s). I won't talk much about binary math here because you should know or should learn about it yourself - it isn't that important to Wi programming.
5 | 3; // 7 (bitwise OR)
5 ^ 3; // 6 (bitwise XOR)
5 & 3; // 1 (bitwise AND)
5 << 1; // 10 (bitwise left shift)
5 >> 1; // 2 (bitwise right shift)
~5; // -6 (bitwise NOT)Length operator # is used to get the length of an indexable value (strings, arrays, and maps):
print(#[1, 2, 3]); // 3
print(#"string"); // 6
print(#{ "x": 10, "y": 20 }); // 2
# Call
Call operator `()` is used to call functions. We'll talk about them soon. It accepts arguments separated by `,`:
```js
my_function(arg1, arg2);Subscript operator [] is used to access elements of collections - strings, arrays and maps.
[1, 2, 3][0]; // 1
"Hello"[0]; // H
{ "x": 10 }["x"]; // 10The field operator . is used to access fields of an object.
my_object.x = 10;
my_object.x; // 10The invocation operator -> is used to call a function on an object and pass object as the first argument. So instead of:
object.function(object, argument);You write:
object->function(argument);Everything about objects and functions will make more sense later after you read about them.
It also works with value types that have a respective module in the standard library, such as arrays, maps, or strings:
"string"->sub(1, 4); // tri
[10]->count(); // 1
{ "a": 1, "b": 2 }->keys(); // ["a", "b"]The new operator is used to clone an object. It creates a new object that is a copy of the original.
var person = object {
name = "";
};
var alice = new person;
alice.name = "Alice";
var bob = new person;
bob.name = "Bob";
print(alice.name); // Alice
print(bob.name); // Bobnew is essential for Wi's prototype-based OOP. We'll talk more about OOP and the objects later.
Next: Control Flow