-
-
Notifications
You must be signed in to change notification settings - Fork 711
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
Binding value require type annotations in some cases. #1176
Comments
Ah yeah I see, it's an issue of type inference here because the trait Agreed that this does not compile: <DisplayList
bind:my_object
>
{&my_object.name}" => "{my_object.age}
</DisplayList> However I'd note that this does compile, because it means that the compiler can correctly infer the type of <DisplayList
bind:my_object
>
{let _: &MyObject = my_object;}
{&my_object.name}" => "{my_object.age}
</DisplayList> Perhaps that's too clever. |
This problem will always occur when writing a component that is generic over some So
Another solution to the problem would be to go the
<MyComponent _phantom={ PhantomData::<T>::default() } /> I have used this pattern for a few library components. The code-block inside the children is clever! Never thought about that. It seems that there is the benefit of not requiring any children prop using the PhantomData prop. |
@vldm Given these two options, do you think there's a need for additional support in the |
@gbj I fully support the idea of separating xml-like part from rust blocks. But I'm not drastically tuned, because I think some simplifications are appropriate (for example, expressions without curly braces in attribute values) Both provided solutions look like hacks to me. Yes, they solve the issue, but they could be not obviuos for users. I think, implementing generics in component is probably a better way to make original code compilable, especially when you consider that in other frameworks this has already been done. (But as @lpotthast mention, it doesn't solve the case where some associated type of generic is used) As for the "binding syntax", i think that the argument for changing it will only be the need to use From rstml perspective, i have plan to implement both: generics and "fn binding syntax", but currently a bit busy with my job, so have no time to implement generics. And if you need any of these features in a leptos view/template - I'd be happy to add supportthese. |
Cool, didn't know Yew did this (allow generics in component names). I'm very comfortable with that solution. Not urgent of course. view! { cx,
<MyGenericComponent<i32> data=123/>
}; |
The solution mentioned above (allow generics on component names) has been implemented in view! {
<MyGenericComponent<i32> data=123/>
}; |
Does this extrapolate out to multiple generics? view! {
<MyGenericComponent<i32, u64, String> data=123, data2=456, data3="Hi".to_string()/>
}; |
@benwis I didn't test it in leptos. But in rstml it can parse array of generics (and even lifetime generics https://docs.rs/syn/2.0.28/syn/struct.Generics.html) In my opinion if it can't in leptos or parser works in controversial way with regular rust generics syntax - it can be assumed as a bug. |
As was proposed in #1140 (comment) creating a new issue with
complete-but-not-compiling example.
In this example, i trying to create a component that request data from external storage, the query is bound to Type as in ORM.
Instead of using real db, i added mocked version, to simlify example.
The full source code can be found at https://github.com/vldm/leptos-non-working-orm/blob/master/src/lib.rs, but to make issue independent i also attach its compact version:
Leptos Dependencies
Please copy and paste the Leptos dependencies and features from your
Cargo.toml
.For example:
To Reproduce
Steps to reproduce the behavior:
cargo check
Screenshots

Related issue
During writing this example, i also found that
#[component]
macro can't implement props builder for this component, because value T is only bound to Fn arguments, so compiller ask to use PhantomData for each unused bound.Possible solutions
But because component is fn not a type, i cant create a type alias.
2. Type in component probably can solve the issue too
I personaly doesn't like
<>
inside tags, because they can confuse users, but yew use it, and i have seen an issue in syn-rsx with proposal to add this.3. writing fn alias with type generics declaration
this is the only one, that work for me in current leptos, can be found at https://github.com/vldm/leptos-non-working-orm/blob/working-fix
But for me it is the most ugly one.
4. New syntax, that i proposed in original dissucssion
bind(my_object: &MyObject)
instead ofbind:my_object
The only problem that i found only one two places where this syntax is required:
<script lang="rust" component:name="Foo" props(initial_value: i32, step: i32)> /* rust code */ <..
There is also other aplication for bind, but usually they are not require any syntax, or syntax that they require is not same as in
ClosureArgs
, some of examples:The text was updated successfully, but these errors were encountered: