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

Remove Component validation in html! to allow for generic components #1065

Merged
merged 2 commits into from
Apr 4, 2020
Merged

Remove Component validation in html! to allow for generic components #1065

merged 2 commits into from
Apr 4, 2020

Conversation

mankinskin
Copy link
Contributor

@mankinskin mankinskin commented Apr 2, 2020

Because this validation check tries to impl Component for the component
type, generic components could not have been used, because the impl has to define the generic type parameter itself, but it can only see the <C> part directly, and thus can not define the type parameters trait bounds properly.

With this change, it is possible to write a component like this, with a generic type parameter with trait bounds.

impl<C> Component for TreeView<C>
    where C: Component,
{
    type Message = Msg;
    type Properties = TreeData<<C as Component>::Properties>;
    ...
    fn view(&self) -> Html {
        if self.props.expanded {
            html! {
                <div>{
                    for self.props.children.iter().cloned()
                            .map(|child| html! {
                                <TreeView<C>
                                    element={child.element}
                                    children={child.children}
                                    expanded={child.expanded}
                                />
                            })
                }</div>
            }
        } else {
            html!{}
        }
    }
    ...
}

See more details in this issue: #1064

@jstarry
Copy link
Member

jstarry commented Apr 2, 2020

Take a look at the tests that we run in CI.. The macro test is going to fail because the compiler error message will be different

@jstarry
Copy link
Member

jstarry commented Apr 2, 2020

Oops, macro tests were not running. Could you please rebase off last master and fix the tests?

@jstarry
Copy link
Member

jstarry commented Apr 2, 2020

Because this validation check tries to impl Component for the component
type, generic components could not have been used.
@mankinskin
Copy link
Contributor Author

Guide: https://github.com/yewstack/yew/blob/master/CONTRIBUTING.md#macro-tests

There is a broken link in the guide for the stderr file.
Because I had some problems, I checked out master at 393eb8 and when I run

TRYBUILD=overwrite cargo test --test macro_test

I get

error: no test target named `macro_test`

Running the tests with

./ci/run_tests.sh

yields

...
error: test failed, to rerun pass '--lib'

Caused by:
  could not execute process `wasm-bindgen-test-runner /home/linusb/git/yew/target/wasm32-unknown-unknown/debug/deps/yew-88efceb071029c6b.wasm` (never executed)

Not sure where this is coming from. Do you have any problems running the tests on the current master?

@jstarry
Copy link
Member

jstarry commented Apr 3, 2020

@mankinskin you'll have to run that command from ./crates/macro

Copy link
Member

@jstarry jstarry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Now you can see why I added that validation check 😉

@mankinskin
Copy link
Contributor Author

Yes indeed, it might be confusing at first, but at least it contains the helpful error at the end :) But it would still be interesting to know how to get the trait bounds of a generic type argument from a proc macro ..
But well, thank you for helping me contribute to this awesome project!

@jstarry
Copy link
Member

jstarry commented Apr 3, 2020

but at least it contains the helpful error at the end :)

Haha yes very true!

But it would still be interesting to know how to get the trait bounds of a generic type argument from a proc macro ..

Yeah, I think it's possible. We can inspect and validate the generics from the component type if present. Just don't have time to do so now :)

@jstarry jstarry merged commit 61e940c into yewstack:master Apr 4, 2020
@jstarry
Copy link
Member

jstarry commented Apr 4, 2020

🎉 released in v0.14.3

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

Successfully merging this pull request may close these issues.

2 participants