In the code below, I want to hide the IdentifiableIndices type but cannot because this type is used as generic constraint of my public init:
import SwiftUI
// vvvvvvvvv cannot be fileprivate because at the bottom this type is use as generic constrain of my public init
/*fileprivate*/struct IdentifiableIndices<Base> where Base: RandomAccessCollection, Base.Element: Identifiable {
typealias Index = Base.Index
struct Element: Identifiable {
let id: Base.Element.ID
let rawValue: Index
func callAsFunction() -> Index { rawValue }
}
fileprivate var base: Base
}
extension IdentifiableIndices: RandomAccessCollection {
var startIndex: Index { base.startIndex }
var endIndex: Index { base.endIndex }
subscript(position: Index) -> Element {
Element(id: base[position].id, rawValue: position)
}
func index(before index: Index) -> Index {
base.index(before: index)
}
func index(after index: Index) -> Index {
base.index(after: index)
}
}
extension ForEach where ID == Data.Element.ID, Data.Element: Identifiable, Content: View {
// vvvvv no can do! and this prevents **fileprivate IdentifiableIndices**: vvvvvvvvvvvv
/*public*/init<T>(indicesOf data: T, @ViewBuilder content: @escaping (T.Index) -> Content) where Data == IdentifiableIndices<T> {
self.init(IdentifiableIndices(base: data)) { index in
content(index())
}
}
}