young
(rtSwift)
1
import SwiftUI
struct PlainGroupBoxStyle: GroupBoxStyle {
func makeBody(configuration: Configuration) -> some View {
VStack(alignment: .leading) {
configuration.label
configuration.content
}
}
}
extension GroupBoxStyle where Self == PlainGroupBoxStyle {
static var plainGroupBoxStyle: PlainGroupBoxStyle { .init() }
}
struct CardGroupBoxStyle: GroupBoxStyle {
func makeBody(configuration: Configuration) -> some View {
VStack(alignment: .leading) {
configuration.label
configuration.content
}
.padding()
.background(Color(.systemGroupedBackground))
.clipShape(RoundedRectangle(cornerRadius: 28, style: .continuous))
}
}
extension GroupBoxStyle where Self == CardGroupBoxStyle {
static var plainGroupBoxStyle: CardGroupBoxStyle { .init() } // `plainGroupBoxStyle` again here, why no error?
}
Autocomplete show both:

The whole thing
import SwiftUI
struct PlainGroupBoxStyle: GroupBoxStyle {
func makeBody(configuration: Configuration) -> some View {
VStack(alignment: .leading) {
configuration.label
configuration.content
}
}
}
extension GroupBoxStyle where Self == PlainGroupBoxStyle {
static var plainGroupBoxStyle: PlainGroupBoxStyle { .init() }
}
struct CardGroupBoxStyle: GroupBoxStyle {
func makeBody(configuration: Configuration) -> some View {
VStack(alignment: .leading) {
configuration.label
configuration.content
}
.padding()
.background(Color(.systemGroupedBackground))
.clipShape(RoundedRectangle(cornerRadius: 28, style: .continuous))
}
}
extension GroupBoxStyle where Self == CardGroupBoxStyle {
static var cardGroupBoxStyle: CardGroupBoxStyle { .init() } // `plainGroupBoxStyle` again here, why no error?
}
struct GroupBoxDemo: View {
var body: some View {
ScrollView {
LazyVGrid(columns: [.init(), .init()]) {
ForEach(0..<10) { _ in
GroupBox(
label: Label("Heart Rate", systemImage: "heart.fill")
.foregroundColor(.red)
) {
Text("Your hear rate is 90 BPM.")
}
.groupBoxStyle(.plainGroupBoxStyle)
}
}.padding()
}
}
}
struct GroupBoxDemo_Previews: PreviewProvider {
static var previews: some View {
GroupBoxDemo()
}
}
toph42
(Topher Hickman)
2
There’s no situation where these extensions apply to the same thing, right? Why do you expect there’s a “collision?”
young
(rtSwift)
3
They are static var, I don't think you can have two duplicated in one protocol?
toph42
(Topher Hickman)
4
What type would be a GroupBoxStyle where Self is both PlainGroupBoxStyle and CardGroupBoxStyle.
young
(rtSwift)
5
Ok, I see now. It's probably bad practice because it prevents implicit static member lookup. But they are two different concrete types and they can be disambiguated with fully qualifying.