2

I am a scala newbie and I was wondering, if builder patterns, like the one described in http://blog.rafaelferreira.net/2008/07/type-safe-builder-pattern-in-scala.html have some usage in scala >= 2.8.

With named parameters and default arguments I can declare which arguments are mandatory (by not giving them default value) and I can pass constructor arguments in any order I want (by using named parameters).

Is there any advantage of having builder then?

Filburt
  • 17,626
  • 12
  • 64
  • 115
Andna
  • 6,539
  • 13
  • 71
  • 120
  • 1
    I believe it is still useful. If you go through the whole tutorial you see advanced features of the pattern like cardinality constraints. Also, it can sometimes be inconvenient to write one monster expression that provides all the parameters in a single call. Lastly, as soon as you put a defaulted formal parameter into a method, some limitations come into effect (e.g., no overloading of that method is allowed). – Randall Schulz Feb 20 '14 at 00:47
  • 1
    (I missed the five-minute edit deadline, so...) If you have multiple defaulted parameters of the same type, it can be ambiguous to leave any out (primitives–especially `boolean`–and ubiquitous types such as `String` seem to be the most often repeated). – Randall Schulz Feb 20 '14 at 00:57
  • @RandallSchulz Apparently, a man achieves a certain reputation and stature and no longer feels the need to post upvotable answers. SO should give fractional points for upped comments, the way bank accounts track fractional pennies. – som-snytt Feb 20 '14 at 04:49
  • @RandallSchulz yes, but if I will always use constructors by passing named parameters I think that the problem you are mentioning is non existant. – Andna Feb 20 '14 at 12:34

2 Answers2

1

The builder pattern allows an object that is partially constructed (according to some arbitrary criteria).

For example, you might have an object that is partially initialized at startup, but requires further parameterization before usage.

It's not possible to express that in a single parameter list (or list thereof), except if one of the args captures the partial configuration (which kind of defeats the utility).

som-snytt
  • 39,429
  • 2
  • 47
  • 129
0

Reason why I want builder pattern :

  1. consistency , since constructor is method and method is convertible with function.

    • strict function -> partial applied function or curried function
    • strict method -> N/A (not allowed in Scala, you need convert it into function but that lose parameter informations.)
    • strict class -> same as above, or use builder pattern to partial applied the constructor.
    • and partial applied type , for example type State[S, A] = StateT[Eval, S, A]
  2. Do you like partial applied function, I do, for functional programming , you are usually passing function around, and you don't want always pass it accompanying with its partial arguments, so you partial applied it and pass just one value along.
    And given 1, it should go the same for method and class.

  3. separation the construction into different place. (similar to 2.)
  4. cache computation. check this answer https://stackoverflow.com/a/4916606/2130573
WeiChing 林煒清
  • 4,452
  • 3
  • 30
  • 65