Conform Never to Identifiable
- Proposal: SE-NNNN
- Authors: Kyle Macomber
- Review Manager: TBD
- Status: Pitch
- Implementation: apple/swift#38103
Introduction
This proposal conforms Never to Identifiable to make it usable as a "bottom type" for generic constraints that require Identifiable.
Motivation and Proposed Solution
With the acceptance of SE-0215, Never was deemed as being a “blessed bottom type”, but that it wouldn’t implicitly conform to all protocols—instead explicit conformance would be added where valuable.
The conformance of Never to Equatable and Hashable in SE-0215 was motivated by examples like using Never as a generic constraint in types like Result and in enumerations. These same use cases motivate the conformance of Never to Identifiable, which is pervasive in commonly used frameworks like SwiftUI.
For example, the new TableRowContent protocol in SwiftUI follows a "recursive type pattern" and has the need for a primitive bottom type with an Identifiable assocated type:
extension Never: TableRowContent {
public typealias TableRowBody /* conforms to TableRowContent */ = Never
public typealias TableRowValue /* conforms to Identifiable */ = Never
}
Detailed design
@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
extension Never: Identifiable {
public var id: Never {
switch self {}
}
}
Source compatibility
If another module has already conformed Never to Identifiable, the compiler will emit a warning:
MyFile.swift: warning: conformance of 'Never' to protocol 'Identifiable' was already stated in the type's module 'Swift'
extension Never: Identifiable {
^
MyFile.swift: note: property 'id' will not be used to satisfy the conformance to 'Identifiable'
var id: Never {
^
As the warning notes, the new conformance will be used to satisfy the protocol requirement. This difference shouldn't present an observable difference given that an instance of Never cannot be constructed.
Effect on ABI stability
This change is additive.
Effect on API resilience
As this change adds new ABI, it cannot be removed in the future without breaking the ABI.
Alternatives considered
Add additional "missing" conformances to Never (e.g. CaseIterable) and other common types
A more thorough audit of "missing" conformances is called for. With this proposal we chose the narrowest possible scope in order to prioritize the addition of important functionality in a timely manner.