0Pricing
Kotlin Academy · Lesson

Building DSL-like APIs

Use infix for clarity.

Building DSL-like APIs is a free Kotlin Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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)
}

Frequently asked questions

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

Yes — the full text of “Building DSL-like APIs” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

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