Redeclare static computed var in protocol: why no error?

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:

Screen Shot 2021-08-10 at 11.13.19 AM

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()
    }
}

There’s no situation where these extensions apply to the same thing, right? Why do you expect there’s a “collision?”

They are static var, I don't think you can have two duplicated in one protocol?

What type would be a GroupBoxStyle where Self is both PlainGroupBoxStyle and CardGroupBoxStyle.

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.

Terms of Service

Privacy Policy

Cookie Policy