Friday, September 22, 2017

Swift Proposal: Non-Exhaustive Enums

Jordan Rose:

Currently, adding a new case to an enum is a source-breaking change, which is very inconvenient for library authors. This proposal aims to distinguish between enums that are exhaustive (meaning they will never get any new cases) and those that are non-exhaustive, and to ensure that clients handle any future cases when dealing with the latter. This change only affects clients from outside the original module.


Public enums can be declared as exhaustive or as nonexhaustive. In Swift 4 mode, the default behavior will be exhaustive for source compatibility; in Swift 5 it will be nonexhaustive.

When a client tries to switch over a nonexhaustive enum, they must include a default case unless the enum is declared in the same module as the switch. In Swift 4 mode, omitting this case will result in a warning; in Swift 5, it will be an error.

Enums imported from C will be nonexhaustive by default, with a new C-side annotation to make them exhaustive. These enums conservatively always have the “cross-module” behavior.

Update (2017-12-20): See SE-0192.

Comments RSS · Twitter

Leave a Comment