What is the Never return type? When to use it over Void ?
What is the Never return type? When to use it over Void ?
The Never return type in Swift is a special type that indicates a function will never return to its caller. This is used to inform the compiler that the function will either terminate the program, enter an infinite loop, or otherwise not complete in a way that allows execution to continue past the function call. The Never type is defined as an enum with no cases, making it impossible to instantiate.
Never Over VoidFunctions That Do Not Return Normally:
Never for functions that are designed to terminate the program or enter an infinite loop. Examples include functions like fatalError(), exit(), and preconditionFailure(), which cause the program to crash or terminate.func crashAndBurn() -> Never {
fatalError("Something very, very bad happened")
}
Compiler Optimization:
Never allows the compiler to optimize the code by understanding that certain code paths are unreachable. This can help in eliminating dead code and improving the overall efficiency of the program.Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.