-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Impl IntoOptPropValue for Option<V> -> Option<T> #1834
Conversation
The only implmented types passed to impl_into_prop macro
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cecton I'd love to hear your opinion on this too. I think it's okay for consistency, but it might encourage users to expose Option<String>
rather than Option<Cow>>
🤔 I'm starting to wonder if it would be possible to add a compiler warning to discourage people using |
Can we even warn about this? As far as I know, there's no way to check if the type is
It has the downside of having to manually create the |
sure Co-authored-by: Simon <[email protected]>
It's not always feasible to create
I think this apply on the component code not on user code, since the user code only use what the component props type. So it's the components developer responsibility to make sure the props type are of |
I think we need to split the problem in 2 concerns we want to address:
Point1: conversionI just made this temporary project: use yew::prelude::*;
struct MyComponent;
impl Component for MyComponent {
type Properties = ();
type Message = ();
fn create(_: <Self as yew::Component>::Properties, _: ComponentLink<Self>) -> Self {
todo!()
}
fn update(&mut self, _: <Self as yew::Component>::Message) -> bool {
todo!()
}
fn change(&mut self, _: <Self as yew::Component>::Properties) -> bool {
todo!()
}
fn view(&self) -> Html {
let option_string: Option<String> = None;
let string: String = String::new();
html! {
<>
<span style=option_string.map(Into::into) />
<span style=string />
</>
}
}
}
fn main() {
println!("Hello, world!");
} Which seems to work fine AND it's very concise:
Now we can debate whether or not this is ergonomic but it seems to me using Point 2: intuitiveness/ergonomicOn intuitivenessRust is a programming language without GC. We use the borrow checker to enforce safety. The alternative to borrowing is reference counting ( Either our API is capable to handle borrowed values (this means having lifetimes everywhere) or we extensively use Because of the requirement of cloning everywhere, it means we are going to clone a lot of unnecessary things like Of course it's not because HTML is stringly typed that we want a web framework API to rely on string typing. That is why it is important to discourage the use of (Side note: one alternative would be to use On ergonomicIt's kinda annoying to type To improve the situation we could improve the error message provided by the macro. As @hamza1311 said it is not possible to check at compile time what the type is. But we can definitely parse the types of the user's own components and detect Possible solution
If we can't do the point 1, than we shouldn't do the point 2. |
While that approach is fine for elements, I'm not sure how well it'd work for components which take @siku2 mentioned on discord that we should have a string type in Yew which is a wrapper around
I'm not sure if that's a good idea because there would be no way mutate it without cloning or interior mutability.
I think we should stay consistent. I don't think If nothing else, I think we should clarify anti-patterns in the documentation. If we can, I wouldn't mind logging a warning to browser's console on use |
Thank you both for your input, @cecton and @hamza1311. |
I think we should document anti-patterns. I would be fine with making a PR for those but first we need to list them.
|
Let's open an issue to track those |
* Impl IntoOptPropValue for Option<V> -> Option<T> The only implmented types passed to impl_into_prop macro * Update packages/yew/src/html/conversion.rs sure Co-authored-by: Simon <[email protected]> Co-authored-by: Simon <[email protected]>
* Impl IntoOptPropValue for Option<V> -> Option<T> The only implmented types passed to impl_into_prop macro * Update packages/yew/src/html/conversion.rs sure Co-authored-by: Simon <[email protected]> Co-authored-by: Simon <[email protected]>
* Impl IntoOptPropValue for Option<V> -> Option<T> The only implmented types passed to impl_into_prop macro * Update packages/yew/src/html/conversion.rs sure Co-authored-by: Simon <[email protected]> Co-authored-by: Simon <[email protected]>
The only implemented types are those passed to impl_into_prop! macro
Description
I need this so I can pass
Option<String>
to some html attributes such asstyle
andid
.Here is an simplified example of what I have:
without this PR I get error saying:
Checklist
cargo make pr-flow