Kotlin
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Null Safety
-
Section 4: Object-Oriented Kotlin
-
Section 5: Functional Kotlin
-
Section 6: Coroutines
-
Section 7: Collections Deep Dive
-
Section 8: Type System Deep Dive
-
Section 9: Interop and Tooling
-
Section 10: Kotlin DSLs and Patterns
-
Section 11: Testing and Quality
-
Section 12: Server-Side Kotlin
-
Section 13: Practical Projects
-
Section 14: Interview Practice
-
Section 15: More Practice Exercises
-
Section 16: More Standard Library
-
Section 17: Multiplatform Kotlin
-
Section 18: kotlin.collections In Depth
-
Section 19: kotlin.text In Depth
-
Section 20: kotlin.ranges and kotlin.sequences
-
Section 21: kotlin.io and File Handling
-
Section 22: kotlinx.coroutines Deep Dive
-
Section 23: kotlin.reflect
-
Section 24: Android Development with Kotlin Overview
-
Section 25: Kotlin Multiplatform Deep Dive
-
Section 26: Kotlin for Backend Deep Dive
-
Section 27: Kotlin Design Patterns
-
Section 28: Advanced Language Features
-
Section 29: More Practice Exercises
-
Section 30: More Interview Practice
-
Section 31: Kotlin Type System Deep Dive
-
Section 32: Kotlin Null Safety Advanced
-
Section 33: Kotlin Testing Deep Dive
-
Section 34: Kotlin Build Tooling Deep Dive
-
Section 35: Kotlin Serialization
-
Section 36: Kotlin Performance Considerations
-
Section 37: Kotlin Native Overview
-
Section 38: Kotlin for Data and Scripting
-
Section 39: More Coroutines Practice
-
Section 40: More Android-Adjacent Patterns
-
Section 41: More Practical Projects
-
Section 42: More Design and Architecture Practice
-
Section 43: Kotlin Language Evolution
-
Section 44: More Interview and Review
-
Section 45: Kotlin Delegation Patterns Deep Dive
-
Section 46: Kotlin Annotations Deep Dive
-
Section 47: Kotlin for Gradle Plugin Development
-
Section 48: Kotlin Concurrency Beyond Coroutines
-
Section 49: Kotlin Compiler Internals
-
Section 50: Real-World Kotlin Case Studies
-
Section 51: Final Practice Projects
-
Section 52: Kotlin for Server-Side Reactive Programming
-
Section 53: More Practice and Drills
-
Section 54: Kotlin Security Practices
123: The Builder Pattern with Kotlin DSLs
If you've spent any time in the Java ecosystem, you're probably intimately familiar with the Builder pattern. It was the standard answer for the "telescoping constructor" problem—where you end up with five different constructors just to handle various combinations of optional parameters. In Java, you'd write a separate Builder class, chain a bunch of .setX().setY() methods, and finally call .build(). It works, but in Kotlin, it feels like we're bringing a knife to a gunfight.
The Java Hangover: Chained Setters
Let's look at how we'd build a complex HttpRequest object the "old" way. I'll show you the pattern I see most often from developers who are still thinking in Java terms. It's not technically "wrong," but it's noisy.
class HttpRequest private constructor(
val url: String,
val method: String,
val headers: Map<String, String>,
val body: String?
) {
class Builder {
private var url: String = ""
private var method: String = "GET"
private val headers = mutableMapOf<String, String>()
private var body: String? = null
fun setUrl(url: String) = apply { this.url = url }
fun setMethod(method: String) = apply { this.method = method }
fun addHeader(key: String, value: String) = apply { headers[key] = value }
fun setBody(body: String) = apply { this.body = body }
fun build() = HttpRequest(url, method, method, headers, body)
}
}
// Usage
val request = HttpRequest.Builder()
.setUrl("https://api.example.com/v1")
.setMethod("POST")
.addHeader("Content-Type", "application/json")
.setBody("{ \"id\": 123 }")
.build()
The problem here isn't the logic; it's the ceremony. You're constantly repeating the builder's context, and the syntax is purely imperative. It reads like a list of instructions rather than a definition of a request. Plus, you're stuck with a mutable builder object hanging around until the final build() call.
Levelling Up to a Type-Safe DSL
Kotlin allows us to turn this into a Domain Specific Language (DSL) by leveraging function literals with receivers. Instead of chaining methods, we can create a block of code where this refers to the builder itself. This transforms the experience from "calling methods on an object" to "configuring an object within a scope."
I prefer this approach because it visually separates the configuration from the execution. Here is how I would refactor that same request logic:
class HttpRequest(
val url: String,
val method: String,
val headers: Map<String, String>,
val body: String?
)
class HttpRequestBuilder {
var url: String = ""
var method: String = "GET"
private val headers = mutableMapOf<String, String>()
var body: String? = null
fun header(key: String, value: String) {
headers[key] = value
}
fun build() = HttpRequest(url, method, headers, body)
}
// This is the magic part: the DSL entry point
fun httpRequest(block: HttpRequestBuilder.() -> Unit): HttpRequest {
return HttpRequestBuilder().apply(block).build()
}
// Usage
val request = httpRequest {
url = "https://api.example.com/v1"
method = "POST"
header("Content-Type", "application/json")
body = "{ \"id\": 123 }"
}
Notice the difference? We've stripped away the .set... prefixes. By using HttpRequestBuilder.() -> Unit, we've told Kotlin that the lambda provided to httpRequest should be executed inside the context of an HttpRequestBuilder instance. It's cleaner, it's more declarative, and it feels native to the language.
When the Magic Becomes a Liability
Now, a word of caution. DSLs are powerful, but they can leak. In the example above, if HttpRequestBuilder had a build() method that was public, a user could technically call build() inside the httpRequest { ... } block, which would be completely nonsensical.
If you're building a complex, nested DSL—say, for a UI layout or a HTML generator—you'll find that the scope can get messy. You might accidentally call a method from an outer builder while you're inside an inner builder. To prevent this, I highly recommend using the @DslMarker annotation. It tells the compiler to forbid calling members of outer receiver scopes from within inner ones. It's a small addition that saves you from hours of debugging "Why is this value being set in the wrong object?"
Essentially, the trade-off is simple: you're exchanging the rigid, explicit nature of the Java Builder for a more flexible, concise syntax. For most internal configuration objects in a Kotlin project, the DSL approach is the clear winner.
📋 Practical Task
Build a Type-Safe Email Composition DSL
You need to create a system for constructing emails. An Email should have a recipient, a subject, a body, and an optional list of attachments. Instead of using a standard constructor, implement this using a Kotlin DSL.
Requirements:
- Create an
Emaildata class to hold the final values. - Create an
EmailBuilderclass that handles the mutable state during construction. - Implement a top-level
email { ... }function that initializes the builder, applies the provided lambda, and returns a completedEmailobject. - The DSL should allow adding multiple attachments using a function (e.g.,
attachment("file.pdf")) rather than adding to a list directly.
Verification: Your code should allow you to instantiate an email like this:
val myEmail = email {
recipient = "dev@example.com"
subject = "Project Update"
body = "The build is finally green!"
attachment("logs.txt")
attachment("screenshot.png")
}There are no comments for now.