🔸Never

定義

// an empty enum
enum Never {}

重要語錄

Never is an uninhabited type, which means that it has no values. Or to put it another way, uninhabited types can’t be constructed. ... Consider a function declared to return an uninhabited type: Because uninhabited types don’t have any values, the function can’t return normally. 👉 NSHipster

Never is very useful for representing impossible code. Most people are familiar with it as the return type of functions like fatalError, but Never is also useful when working with generic classes. For example, a Result type might use Never for its Value to represent something that always errors or use Never for its Error to represent something that never errors. An uninhabited type can be seen as a subtype of any other type — if evaluating an expression never produces a value, it doesn’t matter what the type of that expression is. If this were supported by the compiler, it would enable some potentially useful things … 👉 SE-0215: Conform Never to Equatable and Hashable

遵循協定

參考資料

  • Swift ⟩ Standard Library ⟩ Debugging & Reflection ⟩ Never

Last updated

Was this helpful?