[swift-evolution] [Proposal] Property behaviors

Michel Fortin michel.fortin at michelf.ca
Thu Dec 17 16:23:47 CST 2015


Le 17 déc. 2015 à 16:36, Joe Groff <jgroff at apple.com> a écrit :

>> On Dec 17, 2015, at 1:14 PM, Michel Fortin <michel.fortin at michelf.ca> wrote:
>> 
>> About synchronized access, it seems to me that it'd be advantageous if implementing subscript in a behaviour was optional. This way you can force access of a property through a synchronized block which will help avoid races:
>> 
>> 	var (synchronized) data: (x: Int, y: Int, z: Int)
>> 
>> 	func test() {
>> 		data.synchronize { (data) in
>> 			data.x += 1
>> 			data.y += data.x
>> 			data.z += data.y
>> 		}
>> 	}
> 
> Interesting. IIUC, if a behavior didn't supply a subscript operation, then the property could *only* be accessed through its behavior interface?

Exactly. It's much better than having a `fatalError("Don't access directly")` in the subscript getter.

-- 
Michel Fortin
michel.fortin at michelf.ca
https://michelf.ca



More information about the swift-evolution mailing list