patterns icon indicating copy to clipboard operation
patterns copied to clipboard

Builder pattern: discuss merits of `self` vs `&self` for .build()

Open dodecahedral-champion opened this issue 9 years ago • 4 comments

Suggest changing finish() to use a self receiver instead of &self.

dodecahedral-champion avatar Mar 02 '16 13:03 dodecahedral-champion

Why? I think it is sometimes useful to take &self so that the builder can be used as a template for creating multiple, identical objects. (Of course sometimes it is not possible and then one should use self).

nrc avatar Mar 09 '16 07:03 nrc

I would close this due to inactivity, but perhaps we can use this issue as a suggestion that the Bulder pattern should mention the difference between using &self or self for finish().

cbreeden avatar Sep 23 '16 19:09 cbreeden

Yeah it is probably worth adding some discussion on the relative merits of taking self by value vs by reference in the finish method.

nrc avatar Sep 25 '16 19:09 nrc

@simonsan

I propose to keep both variants and create separate methods to build-by-value and build-by-reference. We can call them build() and build_ref() for example.

mandalorian777 avatar Feb 07 '22 21:02 mandalorian777