Operators
- Operator Precedence
- Mathematical Operators
- Equality & Comparison Operators
- Logical Operators
- The Conditional Operator
- The Null-coalescing Operator
- The Error-coalescing Operator
- Assignment Operators
- Overloading Operators
Operator Precedence
Operator precedence in the table below goes from high at the top to low at the bottom. Operators at the same level have the same precedence.
Level | Operators | Associativity |
---|---|---|
Call |
() [] . : ::
|
Left |
Power |
**
|
Right |
Unary |
! + - ~ try
|
Right |
Bitwise |
& | ^ >> <<
|
Left |
Multiplication |
* / // % mod rem
|
Left |
Addition |
+ -
|
Left |
Comparison |
> >= < <= in
|
Left |
Equality |
== !=
|
Left |
Logical |
&& || ?? !!
|
Left |
Conditional |
:? :|
|
None |
Assignment |
= += -=
|
Right |
Note that conditional expressions using the ternary operator :? :|
can't be nested. (Checkmate, Satan.)
Mathematical Operators
+
|
Addition (binary) or a no-op (unary). Addition returns an integer if both operands are integers or a float if either or both are floats. |
-
|
Subtraction (binary) or negation (unary). Subtraction returns an integer if both operands are integers or a float if either or both are floats. |
*
|
Multiplication. Returns an integer if both operands are integers or a float if either or both are floats. |
/
|
Floating-point division. Both operands will be converted to floats and the result will be a float. |
//
|
Truncating division. Returns an integer if both operands are integers or a float if either or both are floats. |
**
|
Power operator. Both operands are converted to floats and the result is a float. |
mod
|
Modulo operator. Both operands must be integers. |
rem
|
Remainder operator. Returns an integer if both operands are integers or a float if either or both are floats. |
You can overload these mathematical operators to customize their behaviour for your own types.
Note that Pyro has two remainder operators for integer division, rem
and mod
.
-
The
rem
operator returns the remainder left behind by truncated-division. It works like the%
operator in C. -
The
mod
operator returns the remainder left behind by floored-division. It works like the%
operator in Python and corresponds to the modulo operation in mathematics.
Note that Pyro panics on signed-integer overflow — i.e. a mathematical operation on i64
values that would result in signed-integer overflow (undefined behaviour in C) will instead produce a panic.
Equality & Comparison Operators
The equality (==
, !=
) and comparison (>
, >=
, <
, <=
) operators each take two operands and evaluate to a boolean.
- Numbers are equal if their values are numerically equal.
- Strings are equal if they have the same content.
- Tuples are equal if they have the same length and their elements are equal.
- Sets are equal if they are set-equivalent, i.e. if they contain the same items in any order.
- By default, all other objects are equal only if they are the same object.
You can overload the equality and comparison operators to customize their behaviour for your own types.
Logical Operators
The logical operators are ||
(OR), &&
(AND), and !
(NOT). They evaluate the truthiness of their operands.
In Pyro, the values false
, null
, and err
are falsey; all other values are truthy.
The logical operators ||
and &&
are value-preserving and short-circuiting.
The value of the logical-OR expression a || b
is the value of the first operand if that operand is truthy, otherwise the value of the second operand.
This means you can use the ||
operator to swap in a default value in place of a falsey expression, e.g.
var value = maybe_falsey() || "default";
Note that you can chain multiple ||
expressions, e.g.
var value = maybe_falsey() || get_fallback() || "default";
The value of the logical-AND expression a && b
is the value of the first operand if that operand is falsey, otherwise the value of the second operand.
This means you can use the &&
operator to conditionally chain a sequence of expressions, e.g.
func1() && func2();
func2()
will only be called if func1()
returns a truthy value.
Note that you can chain multiple &&
expressions, e.g.
func1() && func2() && func3();
func2()
will only be called if func1()
returns a truthy value; func3()
will only be called if func2()
returns a truthy value.
The Conditional Operator
The conditional operator (a.k.a the "ternary" operator) takes three operands:
<condition> :? <value-if-true> :| <value-if-false>
For example:
echo value > 100 :? "big" :| "small";
The Null-coalescing Operator
The null-coalescing operator ??
lets you swap in a default value in place of a null
:
var value = maybe_null() ?? "default";
The value of the expression a ?? b
is the value of the first operand if that operand is not null
, otherwise the value of the second operand.
You can chain multiple ??
expressions, e.g.
var value = maybe_null() ?? get_fallback() ?? "default";
The Error-coalescing Operator
The error-coalescing operator !!
lets you swap in a default value in place of an error:
var value = maybe_error() !! "default";
The value of the expression a !! b
is the value of the first operand if that operand is not an err
, otherwise the value of the second operand.
You can chain multiple !!
expressions, e.g.
var value = maybe_error() !! get_fallback() !! "default";
Assignment Operators
An assignment expression, a = b
, returns the value assigned, e.g.
var foo; var bar = (foo = 5); assert bar == 5;
Assignment using a compound assignment operator, e.g.
foo += bar;
is equivalent to the longform expression:
foo = foo + bar;
Compound assignment operators cannot be overloaded independently of their base operators — i.e. overloading +
automatically overloads +=
;
Overloading Operators
The following operators can be overloaded by user-defined types:
-
()
-
[]
-
==
-
<
-
<=
-
>
-
>=
-
+
(binary/unary) -
-
(binary/unary) -
*
-
**
-
/
-
//
-
%
-
&
-
|
-
^
-
~
-
<<
-
>>
-
mod
-
rem
You can overload these operators using the $
-prefixed methods listed here.