getset icon indicating copy to clipboard operation
getset copied to clipboard

Fluent Setters

Open danieleades opened this issue 4 years ago • 2 comments

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.

danieleades avatar Jul 25 '21 11:07 danieleades

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.

Hoverbear avatar Aug 03 '21 16:08 Hoverbear

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.

Silberling avatar Jun 24 '22 12:06 Silberling