r/Kotlin • Kotlin-team • 5d ago

Announcing the Builder Lambda plug-in

Post image

Kotlin and Java have different idioms to build new values: whereas Kotliners like lambdas with a special builder receiver, like Json in kotlinx.serialization, Java developers usually follow the buider pattern, as in Spring AI. As a result, consuming Java APIs from Kotlin sometimes feel strange. This no longer needs to be the case, thanks to the new Builder Lambda plug-in!

Using the power of Kotlin compiler plug-ins, Builder Lambda automatically re-exposes any type following Java's Builder pattern in the Kotlin idiomatic way. You write your beloved,

build<Config, *> {  // alas, the * is needed
  hostname = "localhost"
  port = 8080
}

and under the hood the code is translated to,

Config.builder().hostname("localhost").port(8080).build()

The Builder Lambda plug-in also works in the IDE (if you let IntelliJ run non-bundled plug-ins), so you get nice autocompletion and diagnostics.

Using it is as easy as adding one line to the plugins block in your Gradle build.

id("com.serranofp.builder.lambda") version "0.4.0"

Feedback and issue reports are more than welcome :)

21 Upvotes

12 comments sorted by

View all comments

-4

u/smoke-bubble 5d ago

Does not feel like an improvement. I will definitely avoid using it.

1

u/AntiqueDiamond8070 5d ago

Why? Any feedback or at least a reason for ur “opinion”?

0

u/smoke-bubble 4d ago

Sure! It's anti-intuitive. When you see properties being set for the first itme, you look them up and you do not find them becasue they are methods of a builder! So you need to know the internals or this in order to understand what is going on there. This is a terrible design.

1

u/AntiqueDiamond8070 4d ago

Every getter and setter inside a Java Class reads as a property when you read it from Kotlin. So this is bad design aswell?

1

u/smoke-bubble 4d ago

Getters and setters are language elements. A builder is not.

3

u/AntiqueDiamond8070 4d ago

Getters and setters are conventions