HandySwiftUI
HandySwiftUI copied to clipboard
Why not use @ViewBuilder instead of eraseToAnyView?
https://github.com/FlineDev/HandySwiftUI/blob/47dc334071cbee50e84bba7a540954d68acd5a5e/Sources/HandySwiftUI/Modifiers/ViewExt.swift#L234-L245
Is there a specific reason? AnyView seems not good at performance.
@inlinable
@ViewBuilder
func `if`<M: View, A: View>(
_ condition: @autoclosure () -> Bool,
modifier: (Self) -> M,
else alternativeModifier: (Self) -> A
) -> some View {
if condition() {
modifier(self)
} else {
alternativeModifier(self)
}
}
something like this
Oh, sorry, I'm just seeing this right now. Have you tried your suggested change? Does it compile?
I don't remember the exact details of why I implemented it this way, but I think it was simply that the compiler was not able to figure out the return type because it was different based on the condition. IIRC then the fact that modifier returning M and alternativeModifier returning A is the problem, hence the type erasure.
I know it has a performance hit, but I couldn't find another solution. If your @ViewBuilder suggestion solves this somehow (which I didn't try) then it would be good news and a PR would be welcome.