Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 2.0 'inout' function parameters and computed properties

Tags:

swift

swift2

I'm testing Swift 2.0 beta right now and have found strange behaviour. Here is a sample code:

private func someFunc(inout someString: String) {
    print("Inside \'someFunc()\'")

    print(someString)
    someString = "Some another string"
}

private var someAncillaryInt = 42

print(someAncillaryInt)

private var someString: String {
    get {
        print("Inside \'getter\'")
    
        return "Some string"
    }
    set {
        print("Inside \'setter\'")
        someAncillaryInt = 24
    }
}

someFunc(&someString)
print(someAncillaryInt)

Output:

42

Inside 'getter'

Inside 'someFunc()'

Some string

Inside 'setter'

24

I don't understand why wasn't getter called while printing someString inside someFunc() and why was it when someFunc() got passed with someString.

One can assume that I don't understand intricacies of inout parameters yet and after being passed as inout parameter computed property stops being, em, "computed", but why then was 'setter' called when we set another value to someString?

Thanks!

UPD: I added answer below.

UPDATE 18/11/2015: Apple has updated their manual with detailed explanation of how inout params work.

like image 967
mesmerizingr Avatar asked Jul 05 '15 15:07

mesmerizingr


People also ask

How do you pass Inout parameters in Swift?

Swift inout parameter is a parameter that can be changed inside the function where it's passed into. To accept inout parameters, use the inout keyword in front of an argument. To pass a variable as an inout parameter, use the & operator in front of the parameter.

What is Inout parameter in Swift?

You write an in-out parameter by placing the inout keyword right before a parameter's type. An in-out parameter has a value that's passed in to the function, is modified by the function, and is passed back out of the function to replace the original value.

What are Inout parameters?

An INOUT parameter is both an input and an output parameter. You can use the DEFAULT keyword to define the default value for an INOUT parameter as either an expression or NULL.

What are computed properties in Swift?

Computed Properties. In addition to stored properties, classes, structures, and enumerations can define computed properties, which don't actually store a value. Instead, they provide a getter and an optional setter to retrieve and set other properties and values indirectly.


1 Answers

Your confusion might be caused by choosing someString both as the name of a global variable, and as the name of a parameter of the someFunc() function.

print(someString) inside someFunc() prints the value of the (local) function parameter, which is completely unrelated (and hides) the global someString variable.

It becomes easier to understand if you rename the function parameter

private func someFunc(inout localString: String) {
    print("Inside \'someFunc()\'")
    print(localString)
    localString = "Some another string"
}

which is semantically identical (and therefore produces the same output).

You can think of

someFunc(&someString)

as the following:

  • The value of someString is retrieved (using the getter method).
  • someFunc() is executed, with the local parameter localString set to the value of someString.
  • On return from someFunc(), someString is set (using the setter method) to the (possibly changed) value of the local parameter localString.

More information can be found in https://devforums.apple.com/thread/230567 from the Apple Developer Forum, for example:

Given the guarantee of a getter and setter, inout follows naturally: when calling a function with an inout argument, logically it calls the getter on the var/subscript and copies the value into a stack temporary which is guaranteed to have physical addressability. The physical address of the temporary is passed to the inout argument of the function ... . The callee does whatever it wants with that memory location (and never knows whether the thing passed in was computed or not). When the callee returns, the setter is invoked to copy the value back into place.

and

It also guarantees that the getter/setter of a property passed inout will have its getter and setter called once regardless of what the callee does (this is important if the accessors have side effects or are expensive).

but it is also stated that the temporary copy is avoided if necessary.

like image 81
Martin R Avatar answered Sep 30 '22 12:09

Martin R