-
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
137: Practice Exercise: Building a Custom Kotlin DSL for HTML
So, we're diving into one of the coolest parts of Kotlin: building a Domain Specific Language (DSL). You've seen this in libraries like Ktor or Compose, but building your own HTML builder is the best way to actually wrap your head around how the "magic" works. It's basically just a clever combination of extension functions and function literals with receivers.
How do I actually get that nested "tree" structure to work?
The secret is the T.() -> Unit syntax. When you define a function that takes a lambda with a receiver, you're telling Kotlin: "Inside this block, the keyword this refers to an instance of T."
To build an HTML tree, I usually start with a base class for all tags. Here is a simplified version of how I'd set up a basic Tag class and a body builder:
class Tag(val name: String) {
private val children = mutableListOf<Tag>()
var text: String = ""
fun addTag(tag: Tag) {
children.add(tag)
}
override fun toString(): String {
val inner = if (text.isNotEmpty()) text else children.joinToString("") { it.toString() }
return "<${name}>$inner</${name}>"
}
}
class Body : Tag("body") {
fun p(init: P.() -> Unit) {
val p = P().apply(init)
addTag(p)
}
}
class P : Tag("p") {
fun text(value: String) {
this.text = value
}
}
fun html(init: Body.() -> Unit): String {
val body = Body().apply(init)
return "<html>${body}</html>"
}
Now, when you call html { p { text("Hello") } }, you aren't just calling functions; you're configuring an object hierarchy. It feels like a configuration file, but it's actually fully typed code.
What's the best way to handle attributes like "class" or "id"?
You don't want your function signatures to become a nightmare of optional parameters. I've found that the cleanest way is to either use a Map for attributes or create a small helper class. For a simple DSL, adding a property to the Tag class is usually enough to get the job done without over-engineering it.
class Tag(val name: String) {
var attributes = mutableMapOf<String, String>()
// ... previous children logic ...
override fun toString(): String {
val attrString = attributes.entries.joinToString(" ") { "${it.key}=\"${it.value}\"" }
val openTag = if (attrString.isEmpty()) "<${name}>" else "<${name} $attrString>"
// ... rest of toString ...
}
}
// Now we can add a helper function to the Tag class
fun Tag.attr(name: String, value: String) {
this.attributes[name] = value
}
This lets you write p { attr("class", "main-text"); text("Hi") }. It keeps the DSL flexible because you can add any attribute without changing the function definition of every single tag.
Wait, can't I accidentally put a body tag inside a paragraph tag?
Yes, and that's a huge problem. Because of how receivers work, any function available to the parent is also available to the child if you aren't careful. This is where @DslMarker comes in. It's a specialized annotation that tells the compiler: "Don't let me call a function from an outer receiver if I'm already inside an inner receiver of the same marker."
Here is how you'd implement it. First, define your marker:
@DslMarker
annotation class HtmlTagMarker
Then, apply it to all your builder classes:
@HtmlTagMarker
class Body : Tag("body") { ... }
@HtmlTagMarker
class P : Tag("p") { ... }
Now, if you try to call body { p { body { ... } } }, the compiler will throw an error. I highly recommend always using a marker for your DSLs; otherwise, your API becomes a playground for illogical nesting that's a pain to debug later.
📋 Practical Task
Exercise: Building a "Blog Post" Layout DSL
Your task is to extend the HTML DSL concepts from this lesson to create a specialized "Blog Post" builder. You need to implement a DSL that allows a developer to define a blog page with the following constraints:
- The top-level function should be
blogPost { ... }. - Inside
blogPost, you must be able to define exactly onetitletag and onecontentblock. - The
contentblock should allow multipleparagraphtags and at least oneimagetag. - The
imagetag must require asrcattribute to be passed as a parameter to its function (e.g.,image(src = "photo.jpg")). - Use a
@DslMarkerto ensure thattitlecannot be called inside thecontentblock.
Goal: Write the classes and functions necessary so that the following code compiles and produces a string representation of the HTML:
val myPage = blogPost {
title("My Kotlin Journey")
content {
paragraph("Learning DSLs is challenging but rewarding.")
image(src = "kotlin_logo.png")
paragraph("I love how type-safe this is!")
}
}There are no comments for now.