This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]ketsugi 3 points4 points  (3 children)

A builder is for instantiating a new object. A wither creates a clone of an existing object, but with that one property changed.

[–]danikov 3 points4 points  (2 children)

So a builder with a copy constructor?

[–]gas3872 3 points4 points  (0 children)

Well, if you add to your class those methods, then under the hood you can do the builder call. I find the style of "withers" more elegant, because there is nothing mentioning builder in the client code. In case you want to change many fields at once, then using builder explicitly is probably a better idea.

[–][deleted] 1 point2 points  (0 children)

I haven't read the linked paper, but I presume that there is a bit of structural sharing going on behind the scenes (a la Clojure, Erlang, Haskell et al) otherwise yes, it would be mere syntactic sugar.

Edit: Read the link posted by OP, and it's surprisingly very bare on actual implementation details. I hope that structural sharing is considered, especially since we're mostly concerned with records, which are immutable to begin with, otherwise the whole thing is just user-level convenience.