I can get this to (sort of) work using a bunch of AnyView casting
func ifTrue(_ condition:Bool, apply:(AnyView) -> (AnyView)) -> AnyView {
if condition {
return apply(AnyView(self))
}
else {
return AnyView(self)
}
}
and then
struct TestView: View {
@State var showOverlay:Bool = true
@State var activeTap:Bool = true
var body: some View {
Text("Hello, World!")
.ifTrue(showOverlay){
AnyView($0.overlay(Circle().foregroundColor(.red)))
}
.ifTrue(activeTap){
AnyView($0.onTapGesture {
self.showOverlay = !self.showOverlay
})
}
}
}
this includes one or two conversions to AnyView which is ugly and (I believe) bad for performance
can anyone do better?