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

Potential case for optional fallback in <Show> view macro tag #406

Closed
underscorefunk opened this issue Jan 29, 2023 · 3 comments
Closed
Labels
enhancement New feature or request

Comments

@underscorefunk
Copy link
Contributor

The templates tag requires a fallback. There are many cases where conditional data should be displayed with an empty fallback. I'm proposing the idea of making the fallback optional or to have an empty view be the default fallback.

Use cases include display of a sale badge, GDPR consent banners, etc.

Looking forward to hearing what you all think.

@gbj gbj added the enhancement New feature or request label Jan 29, 2023
@benwis
Copy link
Contributor

benwis commented Jan 30, 2023

The question is whether it is clearer to show what the fallback case is, or to create some default.

@underscorefunk
Copy link
Contributor Author

Ah, I was thinking more in terms of, we have a fallback. Do we enable a default fallback of an empty template. Allowing people to set a fallback if they want it, but not requiring a fallback if the fallback is nothingness.

@gbj
Copy link
Collaborator

gbj commented Feb 4, 2023

I think it's a good idea. I went to go do a quick implementation and realized why it's hard :-D

The issue is that we can't have optional generic arguments. If you take a look at the current function signature

pub fn Show<F, W, IV>(
    /// The scope the component is running in
    cx: Scope,
    /// The components Show wraps
    children: Box<dyn Fn(Scope) -> Fragment>,
    /// A closure that returns a bool that determines whether this thing runs
    when: W,
    /// A closure that returns what gets rendered if the when statement is false
    fallback: F,
) -> impl IntoView
where
    W: Fn() -> bool + 'static,
    F: Fn(Scope) -> IV + 'static,
    IV: IntoView,

If we make fallback Option<F>, then in the None case the compiler has no idea what type of a None it is — and there's actually no way to specify it. We could change the fallback type, because then the compiler knows the Option type even in the case of None, so it's fallback: Option<Box<dyn Fn(Scope) -> View>> but that requires the user both to Box the fallback and to add .into_view(cx)

This is one of those Rust things about needing to know how to lay things out in memory and therefore making things slightly less ergonomic. Maybe I'll add a note to the docs: if you want to show nothing for the fallback it's simply fallback=|_| ()

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

No branches or pull requests

3 participants