Operators overview

These following operator groups are listed from the highest priority group to the lowest priority group.

Primary operators

Unary operators

Multiplicative operators

Additive operators

Shift operators

Spaceship operator

Comparison operators

Logical AND operator

Logical XOR operator

Logical OR operator

Equality operator

Conditional AND operator

Conditional OR operator

Null-coalescing operator

Conditional operator

Assignment operators

Assignments result in the new value of x.

Type attribute operators

Ref operators

Operator overloading

Structs and classes can provide operator overloads. Comparison operator selection is flexible, in that not all combination of <, <=, ==, !=, >, and >= need to be defined. The “inverse” of operators will be called if available, or if just the <=> operator is defined then that can be used for all comparison types as well.

struct Vector2
    float x;
    float y;

    public this(float x, float y)
        this.x = x;
        this.y = y;

    public static Vector2 operator+(Vector2 lhs, Vector2 rhs)
        return .(lhs.x, rhs.y);

    public static Vector2 operator-(Vector2 val)
        return .(-val.x, -val.y);

    public static int operator<=>(Vector2 lhs, Vector2 rhs)
        /* Compare on X, or on Y if X's are equal */
        int cmp = lhs.x <=> rhs.x;
        if (cmp != 0)
            return cmp;
        return lhs.y <=> rhs.y;