Really good post. short and clean - like as a Kotlin style ;)
But i think builder could be opened more. Your example is good, but i mean the using of builder is much more then just setting of a properties. Most interesting example, is then u want to pass some code like as parameter. Something from my DSL for selenium SeleniumBuilder:
Using apply for post constructor changes is nice but I wouldn't call it a builder. With the approach you are describing an instance is created first, with empty constructor, and then modified in the apply{} block. This means that
Default values are required
It is not possible to ensure that an instance exists only if the parameters are valid
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Really good post. short and clean - like as a Kotlin style ;)
But i think builder could be opened more. Your example is good, but i mean the using of builder is much more then just setting of a properties. Most interesting example, is then u want to pass some code like as parameter. Something from my DSL for selenium SeleniumBuilder:
Or more colorful example, like as u show, if u want to calc execution time of some code:
thank you!
yeah, those type save builders / DSLs are nice. But they are a topic on their own, and and wanted to keep it "basic" :-)
I would not consider
calcExecTime
a builder, since it does not create anything.btw a method like this already exists in the standard lib - its called
measureTimeMillis
:-)actually yes, its build nothing) sorry for disorientation, but I want to show many other features, who can be provided by lambda with receiver.
Using apply for post constructor changes is nice but I wouldn't call it a builder. With the approach you are describing an instance is created first, with empty constructor, and then modified in the apply{} block. This means that
Default values are required
It is not possible to ensure that an instance exists only if the parameters are valid