Home > Software design >  How to define = extension functions on CGSize
How to define = extension functions on CGSize

Time:01-08

I want to add two CGSize functions using . I can do that simply with

extension CGSize{
    static func  (lhs: Self, rhs: Self) -> CGSize{
        CGSize(width: lhs.width   rhs.width, height: lhs.height   rhs.height)
    }
}

the problem is that it allows me to do let result = size1 size2 but I cannot do size1 = size2. How do I define such that = works?

CodePudding user response:

You must also define the = operator. As an assignment operator, its left-hand-side parameter should be inout and it should return Void.

extension CGSize {
    static func  = (lhs: inout Self, rhs: Self) {
        lhs.width  = rhs.width
        lhs.height  = rhs.height
    }
}

Or, taking advantage of your existing definition of :

extension CGSize {
    static func  = (lhs: inout Self, rhs: Self) {
        lhs = lhs   rhs
    }
}
  •  Tags:  
  • Related