import SwiftUI
struct Foo<Icon: View>: View {
// User provide icon factory, nil for no icon
var icon: (() -> Icon)? // how to default to nil without "Generic parameter 'Icon' could not be inferred"?
var body: some View {
VStack {
Text("Icon:")
icon?()
}
}
}
struct ContentView: View {
var body: some View {
VStack {
Foo(icon: { Image(systemName: "globe")})
Foo<Never>() // Generic parameter 'Icon' could not be inferred
// ^^^^^ anyway to avoid having to do this? nil<Never>?
}
.padding()
}
}
#Preview {
ContentView()
}
I need to use var icon: (() -> Icon)
because the parent needs to provide different icon view depending on parent view state.