[swift-evolution] SE-0030 Property Behaviors

Taras Zakharko taras.zakharko at uzh.ch
Sun Feb 14 06:41:07 CST 2016


You are right! Ok, I’m convinced now as far as that goes :) Smart design. I didn’t think that far, I’m afraid. 

Thanks for explaining again, 

 T. 

> On 14 Feb 2016, at 13:33, Brent Royal-Gordon <brent at architechies.com> wrote:
> 
>> 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
> Architechies
> 



More information about the swift-evolution mailing list