5.4.2 Binary Operators

Arithmetic operators
OperatorOperationOperand 1Operand 2Result type
%moduloFloat/IntFloat/IntFloat/Int
*multiplicationFloat/IntFloat/IntFloat/Int
/divisionFloat/IntFloat/IntFloat
+additionFloat/IntFloat/IntFloat/Int
-subtractionFloat/IntFloat/IntFloat/Int

About the Float/Int return type: If one of the operands is of type Float, the resulting expression will also be of type Float, otherwise the type will be Int. The result of a division is always a Float; use Std.int(a / b) for integer division (discarding any fractional part).

In Haxe, the result of a modulo operation always keeps the sign of the dividend (the left operand) if the divisor is non-negative. The result is target-specific with a negative divisor.

String concatenation operator
OperatorOperationOperand 1Operand 2Result type
+concatenationanyStringString
+concatenationStringanyString
+=concatenationStringanyString

Note that the "any" operand will be stringified. For classes and abstracts stringification can be controlled with user-defined toString function.

Bitwise operators
OperatorOperationOperand 1Operand 2Result type
<<shift leftIntIntInt
>>shift rightIntIntInt
>>>unsigned shift rightIntIntInt
&bitwise andIntIntInt
|bitwise orIntIntInt
^bitwise xorIntIntInt
Logical operators
OperatorOperationOperand 1Operand 2Result type
&&logical andBoolBoolBool
||logical orBoolBoolBool

Short-circuiting:

Haxe guarantees that compound boolean expressions with the same operator are evaluated from left to right but only as far as necessary at run-time. For instance, an expression like A && B will evaluate A first and evaluate B only if the evaluation of A yielded true. Likewise, the expression A || B will not evaluate B if the evaluation of A yielded true, because the value of B is irrelevant in that case. This is important in cases such as this:

if (object != null && object.field == 1) { }

Accessing object.field if object is null would lead to a run-time error, but the check for object != null guards against it.

Compound assignment operators
OperatorOperationOperand 1Operand 2Result type
%=moduloFloat/IntFloat/IntFloat/Int
*=multiplicationFloat/IntFloat/IntFloat/Int
/=divisionFloatFloat/IntFloat
+=additionFloat/IntFloat/IntFloat/Int
-=subtractionFloat/IntFloat/IntFloat/Int
<<=shift leftIntIntInt
>>=shift rightIntIntInt
>>>=unsigned shift rightIntIntInt
&=bitwise andIntIntInt
|=bitwise orIntIntInt
^=bitwise xorIntIntInt

In all cases, a compound assignment modifies the given variable, field, structure member, etc., so it will not work on a read-only value. The compound assignment evaluates to the modified value when used as a sub-expression:

var a = 3;
trace(a += 3); // 6
trace(a); // 6

Note that the first operand of /= must always be a Float, since the result of a division is always a Float in Haxe. Similarly, += and -= cannot accept Int as the first operand if Float is given as the second operand, since the result would be a Float.

Numeric comparison operators
OperatorOperationOperand 1Operand 2Result type
==equalFloat/IntFloat/IntBool
!=not equalFloat/IntFloat/IntBool
<less thanFloat/IntFloat/IntBool
<=less than or equalFloat/IntFloat/IntBool
>greater thanFloat/IntFloat/IntBool
>=greater than or equalFloat/IntFloat/IntBool
String comparison operators
OperatorOperationOperand 1Operand 2Result type
==equalStringStringBool
!=not equalStringStringBool
<lexicographically beforeStringStringBool
<=lexicographically before or equalStringStringBool
>lexicographically afterStringStringBool
>=lexicographically after or equalStringStringBool

Two values of type String are considered equal in Haxe when they have the same length and the same contents:

var a = "foo";
var b = "bar";
var c = "foo";
trace(a == b); // false
trace(a == c); // true
trace(a == "foo"); // true
Equality operators
OperatorOperationOperand 1Operand 2Result type
==equalanyanyBool
!=not equalanyanyBool

The types of operand 1 and operand 2 must unify.

Enums:

  • Enums without parameters always represent the same value, so MyEnum.A == MyEnum.A.
  • Enums with parameters can be compared with a.equals(b) (which is short for Type.enumEq()).

Dynamic:

Comparison involving at least one operand of type Dynamic is unspecified and platform-specific.

Miscellaneous operators
OperatorOperationOperand 1Operand 2Result type
...interval (see range iteration)IntIntIntIterator
=>arrow (see map, key-value iteration, map comprehension)anyany-