๐Ÿ”ธ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