Skip to content

Instantly share code, notes, and snippets.

@bjhomer
Last active May 21, 2016 03:57
Show Gist options
  • Select an option

  • Save bjhomer/08b67ed78ef2cd3d83fe to your computer and use it in GitHub Desktop.

Select an option

Save bjhomer/08b67ed78ef2cd3d83fe to your computer and use it in GitHub Desktop.
Testing whether swift captures by value or by reference.
func testCapturing() -> () {
var x = 3
let c1 = { () -> () in
println( "unmodified x: \(x)")
}
let c2 = { () -> () in
x += 1
println( "modified x: \(x)")
}
c1() // "unmodified x: 3"
c2() // "modified x: 4"
c1() // "unmodified x: 4"
}
testCapturing()
@bjhomer

bjhomer commented Jun 19, 2014

Copy link
Copy Markdown
Author

My reading of the Swift Programming Language Guide suggests that if a closure does not modify a captured variable, then it will be captured by value; that is, the closure will make a separate copy of the variable. If the closure does modify a captured variable, then it is captured by reference.

Given that description, I would expect to see the above print 3, 4, 3. Instead, it prints 3, 4, 4.

Am I misunderstanding the documentation? Or is this just a compiler bug?

@bjhomer

bjhomer commented Jun 19, 2014

Copy link
Copy Markdown
Author

It appears that in Xcode 6 DP2, the behavior does not match the documentation. Instead, captured values are always captured by reference, unless named in a capture list without any capturing modifiers. That is, the following will capture by value:

let c3 = { [x] () -> () in
    println( "captured x: \(x)" )
}

I've filed a bug regarding the mismatch between the documentation and the implementation. Personally, I'd rather see capture-by-value be the default behavior, with capture-by-reference only enabled if named in a capture list.

@PointerFLY

Copy link
Copy Markdown

The guide suggested:
“As an optimization, Swift may instead capture and store a copy of a value if that value is not mutated by a closure, and if the value is not mutated after the closure is created. Swift also handles all memory management involved in disposing of variables when they are no longer needed.”

That means without any side effect, the compiler can store a copy of value in capturing. But since c2 may mutate the value of x, the optimization won't be effective. Programmers do not need to concern the underlying memory management, we could always think it is capture-by-reference during programming. Language features never change, but implementations of compiler may be different.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment