Using inout keyword: is the parameter passed-by-reference or by copy-in copy-out (/call by value result)
Asked Answered
F

1

8

Question: Based on the information and discussion below: Are inout parameters passed-by-reference or by copy-in copy-out?


Based on the following SO threads, function parameters marked by the inout keyword is passed by reference:

We note that the two top-most threads are pre-Swift 2.0; I haven't been able to find any newer discussion on the subject here on SO (except the somewhat related third thread link).


Based on Apple's documentation (as far as I've been able to discern), however, function parameters marked by the inout keyword is passed by copy-in copy-out (or call by value result)

In-out parameters are passed as follows:

When the function is called, the value of the argument is copied. In the body of the function, the copy is modified. When the function returns, the copy’s value is assigned to the original argument. This behavior is known as copy-in copy-out or call by value result. ...

... You write an in-out parameter by placing the inout keyword at the start of its parameter definition. An in-out parameter has a value that is passed in to the function, is modified by the function, and is passed back out of the function to replace the original value. ...


Now for my own example trying to investigate this:

struct MyStruct {
    private var myInt: Int

    mutating func increaseMyInt() {
        myInt++
    }

    func printMyInt() {
        print(String(myInt))
    }

    init(int: Int) {
        myInt = int
    }
}

class MyClass {
    var myStruct: MyStruct

    init(int: Int) {
        myStruct = MyStruct(int: 1)
    }

    func printMyStructsInt() {
        print(String(myStruct.printMyInt()))
    }
}

func myInOutFunc(inout myLocalStruct: MyStruct, myClass: MyClass) -> Int {
    myClass.printMyStructsInt() // prints "1", OK
    myLocalStruct.increaseMyInt()
    myClass.printMyStructsInt() // prints "2": so myStruct is not a copy here?
    myLocalStruct.increaseMyInt()

    return 0
        // according to Apple's doc, shouldn't myStruct member of myClass get
        // assigned (copy of) value of myLocalStruct at this point, and not
        // prior to this?
}

var a = MyClass(int: 1)

a.printMyStructsInt() // prints "1", OK
myInOutFunc(&a.myStruct, myClass: a)
a.printMyStructsInt() // prints "3", OK

This example would imply that inout parameters are indeed passed by reference (as is noted in the two linked SO threads above). Since we prefix the inout parameter with an ampersand (&) this does "feel" logical.

To try my best to make sure that my example is representative---since here inout parameter myLocalStruct is sent as class property---I also made sure that the myLocalStruct didn't get some "behind-the-hood" reference due to it being a class property:

// ... add to bottom of the code above

func testSendStructAsPublicClassProperty(var myLocalStruct: MyStruct) {
    myLocalStruct.increaseMyInt()
}

// test that sending class property doesn't "reference" things up
a.printMyStructsInt() // prints "3"
testSendStructAsPublicClassProperty(a.myStruct)
a.printMyStructsInt() // prints "3", OK (only copy of class property is sent)

Ok, myLocalStruct in this example really function-local, and hence passed by value (no reference-behind-the-hood).


Result: Given the above, inout parameters are passed by reference?

I have two possible follow-up questions:

  1. Have I misinterpreted the description on inout in the Apple language doc, can it be interpreted as "pass by reference"?
  2. Or, is my example still not representative for this case?
Filial answered 21/12, 2015 at 16:49 Comment(0)
A
3

The next two paragraphs in the Language Reference describes it more in detail:

In-Out Parameters

This behavior is known as copy-in copy-out or call by value result. For example, when a computed property or a property with observers is passed as an in-out parameter, its getter is called as part of the function call and its setter is called as part of the function return.

As an optimization, when the argument is a value stored at a physical address in memory, the same memory location is used both inside and outside the function body. The optimized behavior is known as call by reference; it satisfies all of the requirements of the copy-in copy-out model while removing the overhead of copying. Do not depend on the behavioral differences between copy-in copy-out and call by reference.

So it's de facto "pass by reference"

At answered 21/12, 2015 at 17:0 Comment(1)
Ah, I skimmed over that part, thanks! If possible, perhaps you can boldmark also the last sentence in your citation above? It's quite a punch-line w.r.t. the context of my question :)Filial

© 2022 - 2024 — McMap. All rights reserved.