Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator precedence overloading in Swift

Tags:

swift

Is there any way to override operator precedence when overloading operators for custom classes? In my example, + should have higher priority than *. Can I override the default operator precedences?

class Vector{
    var x:Int
    var y:Int
    init(x _x:Int, y _y:Int){
        self.x = _x
        self.y = _y
    }
}

func *(lhs:Vector, rhs:Vector)->Int{
    return lhs.x * rhs.y + rhs.x + rhs.y
}

func +(lhs:Vector, rhs:Vector)->Vector{
    return Vector(x: lhs.x + rhs.x, y: lhs.y + rhs.y)
}

var v1 = Vector(x: 6, y: 1)
var v2 = Vector(x: 3, y: 1)


v1 * v2 + v1
like image 311
Mohsen Avatar asked Dec 19 '22 13:12

Mohsen


2 Answers

Hmm. It actually appears that you can.

operator infix + { associativity left precedence 140 }
operator infix * { associativity left precedence 30 }

let x = 3 + 4 * 5 // outputs 35

But as far as I can tell, this can only be done at "file scope", at least according to a compiler error produced by including this within a class.

'operator' may only be declared at file scope.

like image 90
Mick MacCallum Avatar answered Jan 16 '23 17:01

Mick MacCallum


Answer to @Mohsen 's question

From the publicly available docs here, and under fair use:

Exponentiative (No associativity, precedence level 160)
        << Bitwise left shift
        >> Bitwise right shift

Multiplicative (Left associative, precedence level 150)
        * Multiply
        / Divide
        % Remainder
        &* Multiply, ignoring overflow
        &/ Divide, ignoring overflow
        &% Remainder, ignoring overflow
        & Bitwise AND

Additive (Left associative, precedence level 140)
        + Add
        - Subtract
        &+ Add with overflow
        &- Subtract with overflow
        | Bitwise OR
        ^ Bitwise XOR

Range (No associativity, precedence level 135)
        ..< Half-open range
        ... Closed range

Cast (No associativity, precedence level 132)
        is Type check
        as Type cast

Comparative (No associativity, precedence level 130)
        < Less than
        <= Less than or equal
        > Greater than
        >= Greater than or equal
        == Equal
        != Not equal
        === Identical
        !== Not identical
        ~= Pattern match

Conjunctive (Left associative, precedence level 120)
        && Logical AND

Disjunctive (Left associative, precedence level 110)
        || Logical OR

Ternary Conditional (Right associative, precedence level 100)
        ?: Ternary conditional

Assignment (Right associative, precedence level 90)
        = Assign
        *= Multiply and assign
        /= Divide and assign
        %= Remainder and assign
        += Add and assign
        -= Subtract and assign
        <<= Left bit shift and assign
        >>= Right bit shift and assign
        &= Bitwise AND and assign
        ^= Bitwise XOR and assign
        |= Bitwise OR and assign
        &&= Logical AND and assign
        ||= Logical OR and assign
like image 26
Chris Conover Avatar answered Jan 16 '23 18:01

Chris Conover