Enum
-like cases within Protocols
Swift already supports using enum-like syntax in protocols, for example:
import SwiftUI
extension PrimitiveButtonStyle where Self == BorderlessButtonStyle {
public static var borderless: BorderlessButtonStyle {
BorderlessButtonStyle()
}
}
Button("Log In") {
// Implementation Hidden
}
.buttonStyle(.borderless)
I propose a simpler syntatic sugar that reduces boilerplate and builds off our understanding of how these static properties function.
extension PrimitiveButtonStyle {
case borderless: BorderlessButtonStyle
}
Solved Problems
While nice, the existing implementation is verbose and redundant.
The new syntax removes the confusing where
clause and it fits into the Swift language because the property is already treated like an enum case
is. It's also easier to write and avoids the redundancy of writing the struct name in three places. Under the hood, it would be expanded into the original, retaining the same functionality.