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.
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.
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.
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.
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.
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:
someString
is retrieved (using the getter method).someFunc()
is executed, with the local parameter localString
set to the value of someString
.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With