By Mistake, I named my view 'SegmentedControl', which duplicates SwiftUI type, but there is no compiler error about duplicate type!

So I mistakenly name my view SegmentedControl, which duplicates SwiftUI's:

public struct SegmentedControl<SelectionValue, Content> : View where SelectionValue : Hashable, Content : View {

My code:

import SwiftUI

enum Architecture: String, CaseIterable {
    case mvc
    case mvvm
    case viper
}

//   no compile error here. !!!!
//           vvvvvvvv bad name, should call it something else!!!! 
struct SegmentedControl: View { 
    @State private var selectedArchitecture: Architecture = .viper

    var body: some View {
        Form {
            HStack {
                Text("Architecture:")
                    .bold()
            
                // error here:
                // 'SegmentedControl.Type' is not convertible to '() -> SegmentedControl'
                SegmentedControl(selection: $selectedArchitecture) {
                    ForEach(Architecture.allCases, id: \.self) { architecture in
                        Text(architecture.rawValue).tag(architecture)
                    }
                }
            }
        }
    }
}

#if DEBUG
struct SegmentedControl_Previews: PreviewProvider {
    static var previews: some View {
        SegmentedControl()
    }
}
#endif

But the compiler don't show name conflict error. instead, show

'SegmentedControl.Type' is not convertible to '() -> SegmentedControl'

At the site where I init's a SegmentedControl

Should the compiler give duplicate type name error instead?

Reuse of names is completely valid. The fully qualified names here are actually SwiftUI.SegmentedControl and MyModule.SegmentedControl. For shorthand, the leading module name can be left for the compiler to infer (which is the way most code tends to be written). If you let the compiler choose, it will prefer the current module. I suspect you actually want SwiftUI’s type instead in this line:

SwiftUI.SegmentedControl(selection: $selectedArchitecture) {
    // ...
}

'SegmentedControl.Type' is not convertible to '() -> SegmentedControl'

If you actually are feeding the metatype to something expecting an instance‐returning closure, then this error message is correct and unrelated to the name you chose. On the other hand, if qualifying the type makes this error go away, then the error message is nonsense, and you should report the confusing message as a bug: bugs.swift.org

If you actually are feeding the metatype...

Was just trying to instantiate a SwiftUI SegmentedControl. Not sure why SegmentedControl.Type is in the error message.

On the other hand, if qualifying the type makes this error go away, then the error message is nonsense

Using SwiftUI.SegmentedControl did make to error go away...

Is this a bug?

The error message did not match the actual problem, so it sent you on a wild goose chase. In that respect it is a bug.

To make it clear, it is not a bug for your type to shadow the name of another type in a library. That is completely valid in Swift.

Terms of Service

Privacy Policy

Cookie Policy