0Pricing
Kotlin Academy · Lesson

Building DSL-like APIs

Use infix for clarity.

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

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