Here is an article simplifying builder pattern <h...
# feed
a
How is this simpler than method chaining in typical builder? And you lose the benefit of enforcing invariants via the chaining.
👍 1
r
I don’t really see much value in builders at all when we have named and defaulted parameters.
4
a
With builders you can enforce certain constraints by switching interfaces in the return types of the chain functions. It’s a niche use case though
c
we have named and defaulted parameters
Those are great for end-users, but sadly not good enough for #C8C4JTXR7: adding one is a breaking compatibility change!
g
Not, if you add a parameter with a default
However, I admit, that is will only work in Kotlin-Projects. If you want Java-compatibility, you will need the Builder-Pattern
c
Not, if you add a parameter with a default
That's a binary-breaking change still, it creates an overload that is completely distinct. It is a source-compatible change, so for end users it's ok, but for library authors it's not.
r
Presumably cautious use of
@JvmOverloads
gives some wiggle room? Though I know from experience it’s not a panacea if you have multiple parameters of the same type, so it’s still a hostage to fortune.
c
Yes, but the best way is to manually add an overload so you can annotate it with
DeprecationLevel.HIDDEN
. And even then, you can only add a parameter at the very end of the list.
The builder and DSL patterns don't have these problems. Another pattern that's becoming more and more popular is the "combine objects into each other" pattern. You can see it with Compose's
Modifier
(
Modifier.then(SomeObject).then(SomeOtherObject)
),
CoroutineContext
(
Job() + <http://Dispatchers.IO|Dispatchers.IO> + CoroutineName("foo")
), etc.
👍 1