Kotlin Academy · Lesson

Building DSL-like APIs

Use infix for clarity.

Lesson 2 of 413 steps

Building DSL-like APIs is a free Kotlin Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, and your progress syncs across the web and the CoddyKit app. The Kotlin Academy course includes 4 lessons in total.

Infix for Clarity

Infix functions are a building block of Kotlin DSLs (domain-specific languages). They make API calls read like natural phrases.

infix fun String.eq(value: String) = this == value

fun main() {
    println("on" eq "on")
}

A Fluent Builder Idea

Combining infix with small types lets you express intent declaratively.

class Query(val field: String) {
    infix fun equals(value: String) = "$field = '$value'"
}

fun main() {
    val q = Query("name") equals "Sam"
    println(q)
}

Pair-Based Configuration

The infix to function powers readable configuration maps.

fun configure(vararg opts: Pair<String, Any>) = opts.toMap()

fun main() {
    val cfg = configure("debug" to true, "level" to 3)
    println(cfg)
}

Range DSL

Range builders read like English thanks to infix.

fun main() {
    val r = 1 until 10 step 2
    println(r.toList())
}

Combining Conditions

You can craft readable boolean-like phrases with infix functions.

data class Rule(val ok: Boolean) {
    infix fun and(other: Rule) = Rule(ok && other.ok)
}

fun main() {
    val result = Rule(true) and Rule(false)
    println(result.ok)
}

Lambdas with Receivers

True DSLs often pair infix with lambdas that have receivers, letting a block configure an object directly.

class Html {
    val parts = mutableListOf<String>()
    fun p(text: String) { parts.add("<p>$text</p>") }
}

fun html(block: Html.() -> Unit): Html {
    val h = Html()
    h.block()
    return h
}

fun main() {
    val page = html { p("Hi") }
    println(page.parts)
}

Receiver Lambdas Explained

A function type like Html.() -> Unit makes the block run as if inside the Html instance, so members are callable without a prefix.

class Box { var value = 0 }

fun box(init: Box.() -> Unit): Box {
    val b = Box()
    b.init()
    return b
}

fun main() {
    val b = box { value = 42 }
    println(b.value)
}

Apply as a Mini DSL

The standard apply function is itself a receiver-lambda DSL for configuring an object.

class Server {
    var host = ""
    var port = 0
}

fun main() {
    val s = Server().apply {
        host = "localhost"
        port = 8080
    }
    println("${s.host}:${s.port}")
}

Keep DSLs Readable

A good DSL minimizes noise. Favor names that make the call site read like a description of intent.

data class Style(var bold: Boolean = false)

fun style(block: Style.() -> Unit) = Style().apply(block)

fun main() {
    val s = style { bold = true }
    println(s.bold)
}

Infix Plus Lambda

Combining infix functions with lambdas yields expressive builders.

class Table {
    val rows = mutableListOf<String>()
    infix fun row(text: String) { rows.add(text) }
}

fun table(block: Table.() -> Unit) = Table().apply(block)

fun main() {
    val t = table {
        row("A")
        row("B")
    }
    println(t.rows)
}

When to Build a DSL

DSLs pay off for repetitive, structured configuration (UI, builders, tests). For one-off logic, plain functions are simpler.

fun main() {
    val list = buildList {
        add(1)
        add(2)
    }
    println(list)
}

Quick Check

Test your understanding of DSL-like APIs.

Recap

You learned to build DSL-like APIs:

  • Infix functions make calls read like phrases.
  • Receiver lambdas (Type.() -> Unit) let blocks configure objects directly.
  • apply and buildList are standard-library DSLs.
  • Use DSLs for structured, repetitive configuration; keep them readable.
class C { var x = 0 }
fun c(b: C.() -> Unit) = C().apply(b)

fun main() {
    println(c { x = 9 }.x)
}
Free to start

Learn Kotlin with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Building DSL-like APIs” lesson free?

Yes — any 3 lessons of the Kotlin Academy learning path, including “Building DSL-like APIs”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Kotlin Academy course includes 4 lessons in total.

What will I learn in “Building DSL-like APIs”?

Use infix for clarity. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Academy?

No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “Building DSL-like APIs” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Academy lesson?

Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Infix Functions
  2. Building DSL-like APIs
  3. tailrec Functions
  4. When to Use Each
← Back to Kotlin Academy