Does `_move` into closure argument make any difference?

swift-json has a _move in this closure API:

@inlinable public mutating 
func pop<T>(_ key:String, _ body:(JSON) throws -> T) rethrows -> T?
{
    guard let value:JSON = self.pop(key)
    else 
    {
        return nil
    }
    do 
    {
        #if swift(>=5.8)
        return try body(_move value)
        #else 
        return try body(      value)
        #endif 
    }
    catch let error 
    {
        throw RecursiveError.dictionary(underlying: error, in: key)
    }
}

in light of the discussion `borrow` and `take` parameter ownership modifiers , is there any point in having this _move?

I don't think so. I think this will introduce an unnecessary copy that may be optimized away later by the optimizer. But that is just from reading it quickly.

1 Like

body's function type doesn't declare the parameter to be __owned, so indeed _move doesn't make a difference whether ownership is transfered or not by the call. The move could still be used as a signal to shorten the lifetime of value to end immediately after the call, it's just that that final release will happen after body returns rather than in body itself.

3 Likes