what is~ Copyable, what use cases for it

Asked on 01/28/2025

1 search

The Copyable protocol in Swift is a new feature that describes the ability for a type to be automatically copied. By default, Swift assumes that you want the ability to copy types because it is generally easier to work with copyable types. This means that every type in Swift tries to automatically conform to Copyable, and every generic parameter, protocol, and associated type automatically requires the concrete type to conform to Copyable.

However, there are situations where copying can make your code error-prone. For example, in scenarios like modeling a bank transfer, where running a transfer twice could be costly, it might be better to use non-copyable types to prevent such errors. Non-copyable types are useful for improving program correctness by expressing unique ownership, which is particularly important in cases like managing unique system resources.

For more detailed information, you can refer to the session Consume noncopyable types in Swift (00:00:50) where the concept of copying and non-copyable types is discussed.

what is~ Copyable, what use cases for it | Ask WWDC