Introduction to Move-Only Types in Swift
I thought it would help to have an informal sketch to help outline why move-only types are interesting, clarify a few subtle points (like what “move” really means), and briefly explain some of the issues we’ll need to tackle in order to bring this to Swift.
[…]
So the first step in bringing move-only support to Swift is to add operations with different lifetime-management behaviors. This will include constructs such as
for borrow x in collection
that let you iterate over the items in a collection without requiring an implicit copy andf(take x)
that explicitly invalidates the local value as part of passing it into a function. We’re also exploring variations of these that would allow you to temporarily gain mutable access to a value. These would allow you to efficiently mutate an element “in place” in various scenarios, which is a useful optimization tool for copyable values and an essential prerequisite for move-only values.[…]
By making
Any
a synonym forany Copyable
, we can ensure thatAny
is itself always copyable at the cost of limiting it to only store copyable values. This redefinition would preserve the behavior of current code that usesAny
. Of course, this means we need to introduce a new type that can hold any value whether it is copyable or not.
Previously:
- Swift Pitch: “borrow” and “take” Parameter Ownership Modifiers
- Swift Proposal: Move Function
- Roadmap for Improving Swift Performance Predictability
- On the Road to Swift 6
- Swift Ownership Manifesto