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

Fluent Setters #77

Open
danieleades opened this issue Jul 25, 2021 · 2 comments · May be fixed by #84
Open

Fluent Setters #77

danieleades opened this issue Jul 25, 2021 · 2 comments · May be fixed by #84
Labels

Comments

@danieleades
Copy link

I would like to derive fluent, or 'builder' style API

for my use-case I would prefer settings returning Self, but for other use-cases returning &mut Self is probably more appropriate. ideally both would be supported.

@Hoverbear
Copy link
Collaborator

This seems to be a feature request, you're welcome to propose a syntax and work on a feature, but I don't use this crate, so it's unlikely I'm going to implement this.

@Silberling
Copy link

I'd live to have that feature too. My idea would be this:
A normal setter generates this for example:

#[inline(always)]
pub fn set_public(&mut self, val: T) -> &mut Self {
    self.public = val;
    self
}

where I'd love to use:

#[inline(always)]
pub fn with_public(mut self, val: T) -> Self {
    self.public = val;
    self
}

so that I can do

let mut thing = Thing::new()
    .with_public(value)
    .with_another(123);

So the the differences are the mut self instead of &mut self and the return of Self instead of &mut Self.

There is a pull request #84 which implements this, but I'd be unhappy with the naming. I'd prefer with_ prefix instead of set_ or set_fluent_ (as far as i understand the diff) as this will be short and nobody might trip over the copy of self.

This change will add a great value for getset and will ensure users will write shorter, more readable code when creating structs and builders.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants