Operator precedence overloading in Swift -
Operator precedence overloading in Swift -
is there way override operator precedence when overloading operators custom classes? in example, +
should have higher priority *
. can override 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{ homecoming lhs.x * rhs.y + rhs.x + rhs.y } func +(lhs:vector, rhs:vector)->vector{ homecoming 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
hmm. appears can.
operator infix + { associativity left precedence 140 } operator infix * { associativity left precedence 30 } allow x = 3 + 4 * 5 // outputs 35
but far can tell, can done @ "file scope", @ to the lowest degree according compiler error produced including within class.
'operator' may declared @ file scope.
swift
Comments
Post a Comment