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

Rewrite inline generic bounds #949

Closed
jquesada2016 opened this issue Apr 24, 2023 · 2 comments
Closed

Rewrite inline generic bounds #949

jquesada2016 opened this issue Apr 24, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@jquesada2016
Copy link
Contributor

Currently, the following doesn't compile, but perhaps should:

#[component]
fn MyGenericComponent<T: Debug>(cx: Scope, prop: T) -> impl IntoView {}

The above does not work because the generics need to be rewritten to be:

fn MyGenericComponent<T>(cx: Scope, prop: T) -> impl IntoView
where
  T: Debug
{}

Likewise, this doesn't compile either:

#423 fn Comp(cx: Scope, prop: impl Fn() + 'static) -> impl IntoView {}

In this case, for each impl inline trait, we'd need to rewrite it with some anonymous type name:

fn Comp<__0: Debug>(cx: Scope, prop: __0) -> impl IntoView
where
  __0: Debug
{}
@jquesada2016
Copy link
Contributor Author

In my opinion, the easiest way to implement this, is to use recursive #[component] calls, where the first thing the macro does is to check if there are generics, and if so, rewrite them to the following output:

#[component]
fn Comp(cx: Scop, prop impl Fn()) -> impl IntoView {}

should expand to exactly

#[component]
fn Comp<__0>(cx: Scope, prop: __0) -> impl IntoView
where
  __0: Fn()
{}

This way, the compiler will invoke the macro again and continue as normal.

Of course, in any recursion problem, we need a base case to stop recursion. In this case, we'd just check to see if there are any generics of the previous form we'd need to transform. If there are non, then we continue as normal.

@gbj gbj added the enhancement New feature or request label Apr 24, 2023
@gbj
Copy link
Collaborator

gbj commented Apr 29, 2023

Oh I would genuinely love to have both these generic patterns available: I think impl Trait props would be an especially powerful feature.

Do you want to take a try at this?

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

2 participants