-
-
Notifications
You must be signed in to change notification settings - Fork 11
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
Introduce unstruct macro to help destructuring #39
Conversation
|
Actually, |
sorry I forgot to fill the description on the ticket, will do asap |
yeah I agree but worth the effort, I'll update the description of the PR xD
yeah exactly, you need the name of the fields inside the macro |
I updated the ticket description. It's true that having to write The only remaining advantage of unstruct is that it clones the fields separately. In the end it's really more about development efficiency and convenience. |
It is not forced to be
In function component world, this is done with yew-autoprops. It could become a part of Yew and a missing
*Rust. This is called anonymous structs, and judging by some discussions, this has idea has been thrown out by the community multiple times, no clue why. I guess the main problem is with |
Yup I wanted to keep the sentence simple. It's really only the fields that need to be cheap to clone. And it's not a constraint. I was thinking also about a "derive(ImplicitClone)" the other day to force all the fields to implement ImplicitClone. I don't know if it is really a good idea because it will break quite a lot of code for everybody if we enforce it. Also I'm not sure how to implement this: you can impl ImplicitClone on any struct, even if the fields are not cheap to clone.
cool I didn't know! I don't use much function component though. Usually I go for function component only if there is no state. I like the full fledged components of Yew. But yeah autoprops could definitely clone by default
I mean in JS it doesn't exist. In JS you don't need to specify the type of the object to destructure it.
I guess there are probably valid reasons. But in the context of ImplicitClone and Yew the thing is just plain annoying. For example. I often end up with multiple of them because sometimes I need dereferencing, sometimes I need cloning.
|
I know what you mean now, the repeating-type does not exist; I meant the feature that is missing from Rust doesn't exist in Rust
I don't see much use in any component that has no state... At that point it can be a non-component
I get that you mean struct components, but function components are also full fledged, they just are missing some optimizations, but that's not very important from the user stand point - for them function components can actually do every task at hand, I personally do not see struct components as any more capable (and some Yew discussions focus on getting rid of struct components altogether), more often more cumbersome especially in the link vs hook department.
Not sure what you mean by those examples. Binding via fn update(message: Self::Message) -> bool {
let Props { some_complicated_stuff, enable } = ctx.props().clone();
// some_complicated_stuff: Rc<Stuff>
// enable: bool |
For props in Yew, you can already write the following, which I think is shorter than destructure assignment of anonymous structs. #[function_component]
fn Comp(Props { ref field }: &Props) -> Html {
html! {}
} |
Yes I like that pattern except that it gives references. I often have to extract it from the declaration like this:
Booleans are especially annoying.
Right now, struct component exist, and I prefer to use it over hooks. It's my choice.
Yes in my old code there is a lot of use of references. Nowadays I could just not use references (&), nor dereference (*) but clone all the things. Since the props are cheap to clone, the cost is acceptable for me, booleans will always work, and overall it's much simpler. Well anyway. I think we are digressing quite a lot. To be clear, here are my points:
|
struct Props {
foo: bool,
bar: String,
}
fn doit(&Props {foo, ref bar}: &Props) {
// foo is copied, bar is a reference
} |
@ColonelThirtyTwo the discussion is that
p.s. im not sure whether |
I forgot about this syntax but it doesn't really matter for Yew. Not everything is copy-able primitive.
|
A short demonstrationdemo-out.mp4 |
I'm closing this because I'm tired arguing + I think the syntax proposed here: #39 (comment) is actually the best. Primitives need to be dereferenced, not all types that are implicitclone. I have updated the doc on Yew accordingly to show more of this syntax. I admit I was wrong but I honestly think the attitude of some people here is pretty bad and I am not sure I will want to continue contributing in the future. OSS development is getting tiresome for me. |
I do not think you were wrong in general, I do not know about the others, but personally I was just trying to see if there is any way we can improve it so that experience is the best.
It would be nice to have really, but I think others shown that the way rustfmt and other tools work limit how we can do this properly. Also haven't looked at this syntax that just hit my head: #[unstruct]
pub fn example(#[unstruct] Example1 { a, ref b, ... }: &Example1) {
...
#[unstruct] let Example2 { a, ref b, ... } = example2;
...
} This looks cumbersome at first, since it is Rust double-struct-name syntax + extra |
I'm working with Yew in production since November 2020 and there are a lot of things I think would be great as quality of life improvements.
One of them being the introduction of "ImplicitClone". Every "Properties" struct in Yew should actually be cheap to clone. This is because during rendering the old properties struct is compare with a new one freshly created so it is important that the fields themselves are cheap to clone.
Now that we know that each field of a Properties struct is cheap to clone, we can actually also improve the built-in destructuring of Rust. Rust destructuring has 2 main problems:
With this macro, we can drop the name of the struct plus we don't need to clone all the fields if we don't have too.
I think the macro is better fitted here in implicit-clone rather than Yew but I don't have strong opinion on that.