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

Provide Default deriving #102

Open
ljbade opened this issue Oct 12, 2023 · 4 comments
Open

Provide Default deriving #102

ljbade opened this issue Oct 12, 2023 · 4 comments

Comments

@ljbade
Copy link

ljbade commented Oct 12, 2023

Adding a option to derive Default would make it easier to fill out large structures which are mostly left at None or 0

@gabhijit
Copy link
Collaborator

@ljbade : Thanks for reporting this. I had looked at having a derive Default available in the past. At-least back then in the standard library the default support for Enum types was not present, and while I agree this is handy, finding it during code generation for which to use [derive(Default)] and for which to omit is a bit of a challenge. Probably revisit this and see if this is useful.

In the meanwhile some way this can be handled is -

In your app crate, make this generated code as part of the crate (keep the generated code private or crate-public) and then you can do a derive on that crate.

An example of this can be seen here

I will still revisit the default for Enumsand see if that can be done without additional crate dependencies.

@ljbade
Copy link
Author

ljbade commented Oct 12, 2023

I see what you mean about the default value for Enum, but even if I only had to fill out those then it would still save a lot of effort.

@ljbade
Copy link
Author

ljbade commented Oct 12, 2023

I will try your example, that might work

@gabhijit
Copy link
Collaborator

I see what you mean about the default value for Enum, but even if I only had to fill out those then it would still save a lot of effort.

The issue here is - you have to have all members Default otherwise the compilation of the generated code fails. It's not impossible to add code for impl Default for SomeEnum during code generation, but which of those values is default will be very application specific. Hence I liked the approach mentioned above. Because often for most of the structs some 'utility' impl blocks will be needed.

I will revisit this and see if this can be done in a manner that makes sense for most use cases (it will be opt-in and not opt-out) otherwise close this issue with appropriate reasoning.

Make sense?

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

No branches or pull requests

2 participants