[swift-evolution] SE-0030 Property Behaviors

Brent Royal-Gordon brent at architechies.com
Sun Feb 14 06:33:51 CST 2016

> This means that t1 and t2 should get two different initial values which is probably not what one would have intended.

On the contrary, that's how initial values always work in Swift. Try it yourself: if you copy that code sample, delete the `[custom]`, and paste it into a Swift REPL (any version, as far as I know), it will print "2" and "3".

This semantic is important in several cases, like assigning unique IDs, but it's perhaps most crucial when you're initializing with a reference type. When you write something like this:

	struct Foo {
		let bar = NSMutableArray()

Each `Foo` needs to get its own, separate instance of NSMutableArray, rather than all of them sharing a single array. Reevaluating the initial value for every initialization achieves that goal.

Given that it's the way the rest of the language works, I really don't think it'll be particularly surprising if this semantic carries over to property behaviors, too.

Brent Royal-Gordon

More information about the swift-evolution mailing list