Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why not use @ViewBuilder instead of eraseToAnyView? #3

Open
Josscii opened this issue Nov 24, 2024 · 1 comment
Open

Why not use @ViewBuilder instead of eraseToAnyView? #3

Josscii opened this issue Nov 24, 2024 · 1 comment

Comments

@Josscii
Copy link

Josscii commented Nov 24, 2024

public func applyIf<M: View, A: View>(
_ condition: Bool,
modifier: (Self) -> M,
else alternativeModifier: (Self) -> A
) -> some View {
if condition {
return modifier(self).eraseToAnyView()
}
else {
return alternativeModifier(self).eraseToAnyView()
}
}

Is there a specific reason? AnyView seems not good at performance.

@Josscii
Copy link
Author

Josscii commented Nov 24, 2024

    @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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant