Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
324 views
in Technique[技术] by (71.8m points)

swift2 - Swift 2.0 'inout' function parameters and computed properties

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...