Operators¶
There is a fixed set of operators in Cara:
Unary operators
// ! - ~
!True // -> False | bool negation
-(-123) // -> 123 | number negation
~0b100011 // -> 0b011100 | binary negation
Binary operators
// + - * / % **
// <= < == != > >=
// || &&
// ++
// .. ...
// | & ^ << >> >>>
1 + 2 // -> 3 | addition
3 - 2 // -> 1 | subtraction
2 * 4 // -> 8 | multiplication
4 / 2 // -> 2 | division
5 % 2 // -> 1 | modulo
2 ** 3 // -> 8 | exponentiation
1 <= 3 // -> True | less than or equal
5 < 2 // -> False | less than
3 == 1 // -> False | equals
3 != 1 // -> True | not equal
5 > 2 // -> True | greater than
1 >= 3 // -> False | greater than or equal
True || False // -> True | logical OR
True && False // -> False | logical AND
1 ++ [2,3] // -> [1,2,3] | append
[1,2] ++ [3,4] // -> [1,2,3,4] |
[1,2] ++ 3 // -> [1,2,3] |
1..5 // -> [1,2,3,4,5] | range (inclusive)
1...5 // -> [1,2,3,4] | range (exclusive)
0b1001 | 0b0010 // -> 0b1011 | bitwise OR
0b1001 & 0b0011 // -> 0b0001 | bitwise AND
0b1001 ^ 0b1010 // -> 0b0011 | bitwise XOR
0b1001 << 2 // -> 0b100100 | bitwise shift left
0b1001 >> 2 // -> 0b10 | bitwise shift right (sign-preserving, arithmetic)
0b1001 >>> 2 // | bitwise shift right (unsigned, logical)
Functions¶
Operators are just functions under the hood, and you can access them with backticks (`
):
Overloading¶
All these operators can be overloaded to provide nicer APIs and build DSLs: