Replies: 2 comments 10 replies
-
I think yew should position itself just to be a view library. (Just like react does) So i don't think this should be even part of the Then also I think it should stay external if you are the maintainer and us other maintainers don't show too much interest in maintaining this part of yew core. So would I want to maintain yew immutable js / rs w/e? No, even now if I could I would never even look at
Though i agree that any solutions like your proposed one here are great and the more the merier. Maybe one day we will see that indeed super big majority of users use this and it a logical step to incorporate it into yew. |
Beta Was this translation helpful? Give feedback.
-
This shouldn't be related to Yew at all. What's stopping us from providing these data structures in a Yew-independent way? Let all Rust users benefit from them.
I don't think fully embracing it is the best idea. This causes #1656 which is pain to deal with and ergonomic nightmare. Without language-level support, we can only do so much.
I had an idea of unsafe Copy types where you're on your own. From the outside, the type is cheap to clone. Internally Rc is practically just made of Copy types - |
Beta Was this translation helpful? Give feedback.
-
TL;DR
I'm currently developing an experiment of porting an equivalent of Imutable.js to Yew here: https://github.com/rustminded/yew-immutable/tree/main/examples
The question is: should this be part of Yew's core or an external crate?
Personal background
I've been using Yew for some time from 2 different perspectives:
Why immutability
In a react-like framework, props are passed down to components. Any mutation is done by the parent passing a different prop but you should never mutates your own props.
Why the hidden Rc
In a react-like framework, props are passed down from a parent to a children. When changes in props occur at the parent level, the parent propagate it to its direct children. Because it is the parent that is updated first, we can't provide references of the props to the children. That is why, when using Yew, we need to clone things a lot. We also wrap things in Rc a lot.
I don't think there is a way around this because that's the core design of the react-like framework. Thus I believe we should actually embrace it and add the tools to the user to be able to manage the props easily without having to write Rc everywhere.
Problems solved with this approach
In Yewprint we do have a lot of props in our components and we would prefer the clones to be cheap. In 0.18 we should have used
Cow<'static, str>
everywhere instead of String but it was deeply inconvenient. In 0.19 there is a lot more flexibility thanks toAttrValue
andImplicitClone
developed by @hamza1311. Unfortunately there is still nothing to replaceVec
andHashMap
.I think we can do the extra miles here and create the immutable+rc types for arrays and maps.
Why not other types
String, numbers, arrays and map are kinda primitives when developing a web app. Any other type will still need to be wrapped in an Rc in order to benefit from theses types. This is because I want to guarantee at compile time to the user that the cloning will be cheap.
Primitive types for numbers don't need to have an Rc so those will not be included in this change. But they need to implement
ImplicitClone
in order for this to work.String needs to be place in an Rc.
AttrValue
is already doing a perfect job at that except for theOwned(String)
variant. This is because this one could be uncheap but there is now way to know at compile time.Arrays and Map need their own immutable type too so they can implement
ImplicitClone
and enforce their inner types to implement it as well.Copy vs ImplicitClone
They are actually very alike. The only reason to not use Copy is because you can't implement it arbitrarily.
API
It would probably be beneficial to have an API that reflects JS and Immutable.JS. These are familiar and convenient for JS developers.
WTF THIS IS THE OPPOSITE OF RUST?!
This is. Yes. The first step that put us in this spot is because we are Yew: we imitate JSX and react components. Those are made with a dynamic language in mind and more particularly: with a garbage collector.
Nevertheless, the user will still be able to use their own types everywhere and optimize wherever they can.
Related to #2447
Beta Was this translation helpful? Give feedback.
All reactions