Search code examples
swiftprotocolsinout

A global function calls a mutating method of a protocol type. How can I get rid of the var tmp object?


this is my working code snippet. But I want to get rid of the var tmp variable in the function slide.

protocol Moveable {
    mutating func move(to point: CGPoint)
}

class Car : Moveable {
    var point:CGPoint = CGPoint(x: 23, y: 42)
    func move(to point: CGPoint) {
        self.point = point
        print("Method move from class Car Point: \(point)")
    }
}

struct Shape : Moveable {
    var point:CGPoint = CGPoint(x: 23, y: 42)
    mutating func move(to point: CGPoint) {
       self.point = point
       print("Method move from struct Shape Point:\(point)")
    }
}

var prius: Car = Car()
var square: Shape = Shape()

func slide(slider: Moveable) {
    var tmp = slider  // <---- I want get rid of the tmp object.(
    tmp.move(to: CGPoint(x: 100, y: 100))
}

slide(slider: prius)
slide(slider: square)

I have tried something like that, to avoid the var tmp:

func slide(slider: inout Moveable) {
    slider.move(to: CGPoint(x: 100, y: 100))
}

slider(slider: prius) // <--- Compile Error
// Cannot use mutating member on immutable value: 'slider' is a 'let' constant
slider(slider: prius as (inout Moveable) // <--- Compile Error

Thank you.


Solution

  • It may work with this little change:

    func slide<M: Moveable>(slider: inout M) {
        slider.move(to: CGPoint(x: 100, y: 100))
    }
    

    Instead of asking for Movable we ask for a type conforming to Movable.

    And call it like this:

    slide(slider: &prius)
    slide(slider: &square)