Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reference for Copyable #73238

Merged
merged 7 commits into from
May 22, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions stdlib/public/core/Misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,65 @@ func _rethrowsViaClosure(_ fn: () throws -> ()) rethrows {
try fn()
}

/// A type whose values can be implicitly or explicitly copied.
///
/// Conforming to this protocol indicates that a type's value can be copied;
/// this protocol doesn’t have any required methods or properties.
/// You don't generally need to write an explicit conformance to `Copyable`.
/// The following places implicitly include `Copyable` conformance:
///
/// * Structure declarations,
/// unless it has a noncopyable stored property
/// * Enumeration declarations,
/// unless it has a case whose associated value isn't copyable
/// * Class declarations
/// * Actor declarations
/// * Protocol declarations
/// * Associated type declarations
/// * The `Self` type in a protocol extension
amartini51 marked this conversation as resolved.
Show resolved Hide resolved
///
/// A class or actor can contain noncopyable stored properties,
/// while still being copyable itself ---
/// classes and actors are copied by retaining and releasing references.
///
/// In a declaration that includes generic type parameters,
/// each generic type parameter implicitly includes `Copyable`
/// in its list of requirements.
/// Metatypes and tuples of copyable types are also implicitly copyable,
/// as are boxed protocol types.
/// For example,
/// all of the following pairs of declarations are equivalent:
///
/// ```swift
/// struct MyStructure { }
/// struct MyStructere: Copyable { }
///
/// protocol MyProtocol { }
/// protocol MyProtocol: Copyable { }
///
/// protocol AnotherProtocol {
/// associatedtype MyType
/// associatedtype MyType: Copyable
/// }
///
/// func genericFunction<T>(t: T) { }
/// func genericFunction<T>(t: T) where T: Copyable { }
///
/// let x = any MyProtocol
/// let x = any MyProtocol & Copyable
/// ```
///
/// To suppress an implicit conformance to `Copyable` you write `~Copyable`.
/// For example,
/// only copyable types can conform to `MyProtocol` in the example above,
/// but both copyable and noncopyable types
/// can conform `NoRequirements` in the example below:
///
/// ```swift
/// protocol NoRequirements: ~Copyable { }
/// ```
///
/// Extensions to the `Copyable` protocol are not allowed.
@_marker public protocol Copyable {}

@_marker public protocol Escapable {}
Expand Down