Enumerationswift 6.0.1Swift
Never
A type that has no values and can’t be constructed.
@frozen enum Never
Use Never
as the return type of a function that doesn’t return normally — for example, because it runs forever or terminates the program.
// An infinite loop never returns.
func forever() -> Never {
while true {
print("I will print forever.")
}
}
// Calling fatalError(_:file:line:) unconditionally stops the program.
func crashAndBurn() -> Never {
fatalError("Something very, very bad happened")
}
A function that returns Never
is called a nonreturning function. Closures, methods, computed properties, and subscripts can also be nonreturning.
There’s no way to create an instance of Never
; this characteristic makes it an uninhabited type. You can use an uninhabited type like Never
to represent states in your program that are impossible to reach during execution. Swift’s type system uses this information — for example, to reason about control statements in cases that are known to be unreachable.
let favoriteNumber: Result<Int, Never> = .success(42)
switch favoriteNumber {
case .success(let value):
print("My favorite number is", value)
}
In the code above, favoriteNumber
has a failure type of Never
, indicating that it always succeeds. The switch statement is therefore exhaustive, even though it doesn’t contain a .failure
case, because that case could never be reached.
Citizens in Swift
Conformances
protocol BitwiseCopyable
protocol Comparable
A type that can be compared using the relational operators
<
,<=
,>=
, and>
.protocol Copyable
A type whose values can be implicitly or explicitly copied.
protocol Decodable
A type that can decode itself from an external representation.
protocol Encodable
A type that can encode itself to an external representation.
protocol Equatable
A type that can be compared for value equality.
protocol Error
A type representing an error value that can be thrown.
protocol Escapable
protocol Hashable
A type that can be hashed into a
Hasher
to produce an integer hash value.protocol Identifiable<ID>
A class of types whose instances hold the value of an entity with stable identity.
protocol Sendable
Type members
Instance members
Type features
static func != (lhs: Self, rhs: Self
) -> Bool Returns a Boolean value indicating whether two values are not equal.
static func ... (minimum: Self
) -> PartialRangeFrom<Self> Returns a partial range extending upward from a lower bound.
static func ... (maximum: Self
) -> PartialRangeThrough<Self> Returns a partial range up to, and including, its upper bound.
static func ... (minimum: Self, maximum: Self
) -> ClosedRange<Self> Returns a closed range that contains both of its bounds.
static func ..< (maximum: Self
) -> PartialRangeUpTo<Self> Returns a partial range up to, but not including, its upper bound.
static func ..< (minimum: Self, maximum: Self
) -> Range<Self> Returns a half-open range that contains its lower bound but not its upper bound.
static func <= (lhs: Self, rhs: Self
) -> Bool Returns a Boolean value indicating whether the value of the first argument is less than or equal to that of the second argument.
static func > (lhs: Self, rhs: Self
) -> Bool Returns a Boolean value indicating whether the value of the first argument is greater than that of the second argument.
static func >= (lhs: Self, rhs: Self
) -> Bool Returns a Boolean value indicating whether the value of the first argument is greater than or equal to that of the second argument.
Available in Synchronization
Conformances
protocol AtomicRepresentable
A type that supports atomic operations through a separate atomic storage representation.
Typealiases
Type members
Available in FoundationEssentials
Conformances
protocol SortComparator<Compared>
A comparison algorithm for a given type.