Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@superarts
Created May 7, 2019 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save superarts/7364f376b93f516b46f2f27a121b7268 to your computer and use it in GitHub Desktop.
Save superarts/7364f376b93f516b46f2f27a121b7268 to your computer and use it in GitHub Desktop.
Pure Functions in Swift Enum
protocol PureEnumProtocol {
func test()
}
enum PureEnum: PureEnumProtocol {
static var v = 1 // Still possible
//var v = 1 // Enums must not contain stored properties
func test() {
//v = 2 // Impossible
print("PureTest", PureEnum.v)
}
// Redundant stub initializer is needed
case pure
init() {
self = .pure
}
}
let pureEnum: PureEnumProtocol = PureEnum()
pureEnum.test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment