Is there anyway to name scope a generic protocol type?

I have this:

protocol Selectable: Identifiable {
    associatedtype Label : View
    var isSelected: Bool { get set }
    var label: Label { get }
}

struct Multiselect<T: Selectable>: View {
    @Binding var items: [T]

    var body: some View {
        List {
            ForEach($items) { index, item in
                Toggle(isOn: item.isSelected) {
                    item.wrappedValue.label
                }
            }
        }
    }
}

Selectable is too common/general, nesting it inside Multiselect, if possible would be ideal, but it's not allowed.

I want to avoid naming it MultiselectSelectable

Is there any good solution?

No, you can't declare nested protocols (and you can't declare nested types in protocols too). There has been discussion about this in the past, e.g. in Nested types in protocols (and nesting protocols in types) - #13 by Slava_Pestov.

1 Like