[swift-evolution] [Concurrency] async/await + actors

Michel Fortin michel.fortin at michelf.ca
Sat Aug 19 08:16:49 CDT 2017


>> For instance, has Array<UIView> value semantics?
> 
> By the commonly accepted definition, Array<UIView> does not provide value semantics.
> 
>> You might be tempted to say that it does not because it contains class references, but in reality that depends on what you do with those UIViews.
> 
> An aspect of the type (“does it have value semantics or not”) should not depend on the clients.  By your definition, every type has value semantics if none of the mutating operations are called :-)

No, not mutating operations. Access to mutable memory shared by multiple "values" is what breaks value semantics. You can get into this situation using pointers, object references, or global variables. It's all the same thing in the end: shared memory that can mutate.

For demonstration's sake, here's a silly example of how you can give Array<Int> literally the same semantics as Array<UIView>:

	// shared UIView instances in global memory
	var instances: [UIView] = []

	extension Array where Element == Int {

		// append a new integer to the array pointing to our UIView instance
		func append(view: UIView) {
			self.append(instances.count)
			instances.append(newValue)
		}

		// access views pointed to by the integers in the array
		subscript(viewAt index: Int) -> UIView {
			get {
				return instances[self[index]]
			}
			set {
				self[index] = instances.count
				instances.append(newValue)
			}
		}
	}

And now you need to worry about passing Array<Int> to other thread. ;-)

It does not really matter whether the array contains pointers or wether it contains indices into a global table: in both cases access to the same mutable memory is accessible through multiple copies of an array, and this is what breaks value semantics.

Types cannot enforce value semantics. Its the functions you choose to call that matters. This is especially important to realize in a language with extensions where you can't restrict what functions gets attached to a type.


>> If you treat the class references as opaque pointers (never dereferencing them), you preserve value semantics. You can count the elements, shuffle them, all without dereferencing the UIViews it contains. Value semantics only end when you dereference the class references. And even then, there are some exceptions.
> 
> I agree with you that the model could permit all values to be sent in actor messages, but doing so would give up the principle advantages of mutable state separation.  You’d have to do synchronization, you’d have the same bugs that have always existed, etc.

What the compiler should aim at is enforcing useful rules when it comes to accessing shared mutable state.


-- 
Michel Fortin
https://michelf.ca

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170819/57168b69/attachment.html>


More information about the swift-evolution mailing list