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

Add default value for properties #879

Closed
wants to merge 2 commits into from
Closed

Add default value for properties #879

wants to merge 2 commits into from

Conversation

AlephAlpha
Copy link
Contributor

Fix #775

We will be able to provide default values to properties, using a syntax like:

#[derive(Properties)]
struct MyProps {
    #[props(default = "50 + 50")]
    value: u32,
    #[props(default = "true")]
    enabled: bool
}

The syntax is based on @mdtusz's suggestion, but it takes an expression instead of a path to a function.

value: i32,
}

fn required_prop_generics_should_work() {
Copy link
Contributor Author

@AlephAlpha AlephAlpha Jan 17, 2020

Choose a reason for hiding this comment

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

Sorry, I forgot to change the function name when copying and pasting. It should be something like default_value_should_work.

@mdtusz
Copy link
Contributor

mdtusz commented Jan 17, 2020

Nice work! I'm not sure how I feel about the string expressions though. It seems like it would quickly become not-so-nice for non-primitives, and might invite "bad" usage patterns.

Part of my thinking behind the string-to-function approach was just to somewhat mirror how serde works.

Thoughts @jstarry @hgzimmerman?

@hgzimmerman
Copy link
Member

I personally would prefer if the default value was specified by a path/identifier to a no-argument function that returns the expected type. I do think that this is a good start though!

@jstarry
Copy link
Member

jstarry commented Jan 19, 2020

I think we can close this, I also prefer #881

@AlephAlpha AlephAlpha closed this Jan 19, 2020
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.

Add default value for properties
4 participants