Skip to content

Operators

Arithmetic Operators

OperatorMeaningOperand TypesResult Type
+Additionint, intint
+String concatenationstr, strstr
-Subtractionint, intint
*Multiplicationint, intint
/Division (truncates toward zero)int, intint
%Remainderint, intint

The + operator works as addition for integers and concatenation for strings. The behavior is determined by the left operand's type. Mixing types (e.g., int + str) or using bool operands with + causes a runtime error.

mermaid
flowchart TD
    Start --> A[println 10 + 3]
    A --> B[println 10 - 3]
    B --> C[println 10 * 3]
    C --> D[println 10 / 3]
    D --> E[println 10 % 3]
    E --> F[println 'Hello, ' + 'World!']
    F --> End
console
$ merx run arith.mmd
13
7
30
3
1
Hello, World!

INFO

  • Division by zero causes a runtime error.
  • Addition, subtraction, and multiplication wrap around on overflow.
  • The % operator follows Rust semantics: -10 % 3 == -1.

Comparison Operators

OperatorMeaningOperand TypesResult Type
<Less thanint, intbool
<=Less than or equalint, intbool
>Greater thanint, intbool
>=Greater than or equalint, intbool

Equality Operators

OperatorMeaningOperand TypesResult Type
==Equalanybool
!=Not equalanybool

Comparing values of different types always results in false for == and true for !=.

Logical Operators

OperatorMeaningOperand TypesResult Type
&&Logical ANDbool, boolbool
||Logical ORbool, boolbool

WARNING

Both sides are always evaluated. There is no short-circuit evaluation.

Unary Operators

OperatorMeaningOperand TypeResult Type
-Arithmetic negationintint
!Logical NOTboolbool

Unary operators can be chained: --x, !!b.

Operator Precedence

Operators are listed from highest to lowest precedence. Operators at the same level are left-associative.

PrecedenceOperatorsDescription
1 (highest)- (unary), !Unary operators
2asType cast
3*, /, %Multiplication, division, remainder
4+, -Addition, subtraction
5<, <=, >, >=Comparison
6==, !=Equality
7&&Logical AND
8 (lowest)||Logical OR

Use parentheses () to override precedence.