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

Support special setters for collections #190

Open
Veetaha opened this issue Nov 10, 2024 · 2 comments
Open

Support special setters for collections #190

Veetaha opened this issue Nov 10, 2024 · 2 comments
Labels
feature request A new feature is requested

Comments

@Veetaha
Copy link
Contributor

Veetaha commented Nov 10, 2024

This issue is based on prior thoughts in the alternatives section of the docs.

Design

I don't have a good design for the attribute yet. There are many unresolved questions.

  • What should be the name of the attribute? The name should make it clear that it generates setter(s?) that extend a collection (push (front or back? for VecDeque), insert).
  • Where should the attribute reside? Should it be at the level of #[builder(attr_name)] or #[builder(setters(attr_name))]?
  • Should the setter accept Self by &mut or by value? The typestate won't change after a value is pushed into a colleciton, so &mut self is possible. However by value seems like a more natural fit to the current design where the setters always require self by value.
    • Maybe we should generate a couple of setters for &mut and by-value self?
    • What name pattern would this couple of setters use?
    • How much configrability should we provide for this?

Do We Need This At All?

How often would people want such setters? If that would be needed at a rare occasion, then people could use custom fields (#189) with custom methods to implement this.

I'll keep this issue on hold

Prior Art

A note for the community from the maintainers

Please vote on this issue by adding a 👍 reaction to help the maintainers with prioritizing it. You may add a comment describing your real use case related to this issue for us to better understand the problem domain.

@Veetaha Veetaha added the feature request A new feature is requested label Nov 10, 2024
@andrzejressel
Copy link

In Java there is lombok - they do it by separate annotation @Singular on field, that can take a string that would be new method name.

@Veetaha
Copy link
Contributor Author

Veetaha commented Nov 24, 2024

I've published a 3.1 release of bon that adds #[builder(field)] annotation allowing you to define custom fields. This can be used in combination with custom methods to define special setters for collections. I believe this may be good enough for many use cases since it provides great flexibility to how you'd like these special setters to look like.

See the example of this feature in the new Custom Fields guide.

@andrzejressel do you think #[builder(field)] would cover your use case, or would you like to have some attribute to generate setters for collections via bon? IIRC you are using bon in generated code, so defining custom fields/methods could be fine in this context, since you may define their template in one place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request A new feature is requested
Projects
None yet
Development

No branches or pull requests

2 participants