Thursday, December 19, 2019 [Tweets] [Favorites]

Swift Evolution Pitch: Modify Accessors

Ben Cohen:

We propose the introduction of a new keyword, modify, for implementing mutable computed properties and subscripts, alongside the current get and set.

The bodies of modify implementations will be coroutines, and they will introduce a new contextual keyword, yield, that will be used to yield a value to be modified back to the caller. Control will resume after the yield when the caller returns.

This modify feature is currently available (but not supported) from Swift 5.0 as _modify, for experimentation purposes when reviewing this proposal.

[…]

We cannot yield the value in the array’s buffer directly because it needs to be placed inside an optional. That act of placing inside the optional creates a copy.

We can work around this with some lower-level unsafe code. If the implementation of Array.first has access to its underlying buffer, it can move that value directly into the optional, yield it, and then move it back[…] During the yield to the caller, the array is in an invalid state: the memory location where the first element is stored is left uninitialized, and must not be accessed. This is safe due to Swift’s rules preventing conflicting access to memory.

Previously:

4 Comments

I'm worried about the future of Swift. They seem to be constantly expanding the language to every direction. 2 keywords for this? I would expect something so heavyweight to be reserved for something bigger, like async/await.

I wish they would focus on fixing the bugs instead of chasing new things all the time. The compiler still crashes daily for me and I hit bugs in the language or compiler a few times a week.

Sören Nils Kuklau

My concern is that Swift feels like "C#, but harder". They've been rapidly evolving it by adding more features (though they still don't seem to have a good concurrency story?), but aside from A's quality concerns which I can't comment on, I worry that not enough effort is put into making the basics of the language more accessible to newcomers. It's supposed to be the mainstream way to build a macOS or iOS app, and it doesn't quite feel like it can scale down like that.

@Nick @A I do think this is an important addition to the language. But, yes, the compiler still seems buggy/crashy.

Stay up-to-date by subscribing to the Comments RSS Feed for this post.

Leave a Comment