[swift-evolution] [Pitch] inout with capture variable

Mark Lacey mark.lacey at apple.com
Wed Oct 5 23:30:51 CDT 2016


> On Oct 5, 2016, at 9:06 PM, Cao Jiannan via swift-evolution <swift-evolution at swift.org> wrote:
> 
> for example:
> 
> var a = 1
> 
> let block = { [inout a] in
> 	a += 1
> }
> 
> block()
> block()
> 
> print(a) // 3

This is already how captures work by default in closures and nested functions in Swift:

var a = 1

let block = { a += 1 }

block()
block()

print(a) // prints 3


If you want to capture something immutably you can put it in the capture list:

var a = 1
let block = { [a] in a += 1 } // error: left side of mutating operator isn't mutable: 'a' is a 'let' constant

Mark

> 
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list