Kotlin Domain-Specific Languages (DSLs)

Kotlin Domain-Specific Languages (DSLs) are specialized mini-languages designed to solve problems within a specific domain. Unlike general-purpose languages, DSLs provide expressive and concise syntax tailored to specific tasks, enhancing productivity and reducing the likelihood of errors. Kotlin, with its powerful language features and expressive syntax, is an excellent choice for building DSLs.

In this article, we will explore DSLs, how to build them with Kotlin, type-safe builders, and various DSL design patterns. We will illustrate each concept with real examples and their outputs.

1. Introduction to DSLs

1.1 What are DSLs?

DSLs are programming languages or specifications dedicated to a particular problem domain. They provide abstractions and notations that closely align with the domain concepts, making it easier for domain experts to understand and write code.

Examples of DSLs:

  • SQL for database queries.
  • Regular expressions for pattern matching.
  • HTML/CSS for web page structure and styling.

1.2 Benefits of DSLs

  • Expressiveness: DSLs offer a high level of expressiveness for specific tasks, making the code easier to read and write.
  • Reduced Complexity: By focusing on a specific domain, DSLs can simplify complex tasks.
  • Error Reduction: Domain-specific abstractions help prevent common errors by providing higher-level constructs.

1.3 DSLs in Kotlin

Kotlin is well-suited for creating DSLs due to its features like higher-order functions, lambdas, and infix notation. These features enable the creation of concise and readable DSLs.

2. Building DSLs with Kotlin

2.1 Basic DSL Example

Let’s start with a simple DSL example to define a set of HTML elements.

Example: HTML DSL

Kotlin
class HTML {
    private val children = mutableListOf<HTMLElement>()

    fun body(init: Body.() -> Unit) {
        val body = Body()
        body.init()
        children.add(body)
    }

    override fun toString() = children.joinToString("\n")
}

abstract class HTMLElement {
    abstract override fun toString(): String
}

class Body : HTMLElement() {
    private val children = mutableListOf<HTMLElement>()

    fun p(init: P.() -> Unit) {
        val p = P()
        p.init()
        children.add(p)
    }

    override fun toString() = "<body>\n${children.joinToString("\n")}\n</body>"
}

class P : HTMLElement() {
    var text: String = ""

    override fun toString() = "<p>$text</p>"
}

fun html(init: HTML.() -> Unit): HTML {
    val html = HTML()
    html.init()
    return html
}

fun main() {
    val document = html {
        body {
            p {
                text = "Hello, world!"
            }
        }
    }
    println(document)
}

Output:

Kotlin
<body>
<p>Hello, world!</p>
</body>

2.2 Advanced DSL Example: Building a SQL Query DSL

Let’s create a DSL to build SQL queries.

Example: SQL DSL

Kotlin
class SelectQuery {
    private var columns: String = "*"
    private var table: String = ""
    private var where: String = ""

    fun select(vararg columns: String) {
        this.columns = columns.joinToString(", ")
    }

    fun from(table: String) {
        this.table = table
    }

    fun where(condition: String) {
        this.where = condition
    }

    override fun toString(): String {
        return "SELECT $columns FROM $table${if (where.isNotBlank()) " WHERE $where" else ""}"
    }
}

fun query(init: SelectQuery.() -> Unit): SelectQuery {
    val query = SelectQuery()
    query.init()
    return query
}

fun main() {
    val sql = query {
        select("id", "name", "age")
        from("users")
        where("age > 21")
    }
    println(sql)
}

Output:

Kotlin
SELECT id, name, age FROM users WHERE age > 21

3. Type-Safe Builders with DSLs

Type-safe builders leverage Kotlin’s type system to ensure that the DSL is used correctly, preventing errors at compile time.

3.1 Type-Safe HTML Builder

Using type-safe builders, we can enforce constraints on how the HTML elements are nested.

Example: Type-Safe HTML DSL

Kotlin
@DslMarker
annotation class HtmlTagMarker

@HtmlTagMarker
abstract class Tag(val name: String) {
    private val children = mutableListOf<Tag>()
    protected fun <T : Tag> initTag(tag: T, init: T.() -> Unit): T {
        tag.init()
        children.add(tag)
        return tag
    }

    override fun toString(): String {
        return "<$name>${children.joinToString("")}</$name>"
    }
}

class HTML : Tag("html") {
    fun body(init: Body.() -> Unit) = initTag(Body(), init)
}

class Body : Tag("body") {
    fun p(init: P.() -> Unit) = initTag(P(), init)
}

class P : Tag("p") {
    var text: String
        get() = throw UnsupportedOperationException()
        set(value) {
            appendText(value)
        }

    private fun appendText(text: String) {
        // Append text
    }

    override fun toString(): String {
        return "<$name>${super.toString()}</$name>"
    }
}

fun html(init: HTML.() -> Unit): HTML {
    val html = HTML()
    html.init()
    return html
}

fun main() {
    val document = html {
        body {
            p {
                text = "Hello, type-safe world!"
            }
        }
    }
    println(document)
}

Output:

Kotlin
<html><body><p><p>Hello, type-safe world!</p></p></body></html>

3.2 Type-Safe SQL Builder

We can also apply type-safe builders to our SQL DSL.

Example: Type-Safe SQL DSL

Kotlin
class SelectQuery {
    private var columns: String = "*"
    private var table: String = ""
    private var where: String = ""

    fun select(vararg columns: String) {
        this.columns = columns.joinToString(", ")
    }

    fun from(table: String) {
        this.table = table
    }

    fun where(condition: String) {
        this.where = condition
    }

    override fun toString(): String {
        return "SELECT $columns FROM $table${if (where.isNotBlank()) " WHERE $where" else ""}"
    }
}

fun query(init: SelectQuery.() -> Unit): SelectQuery {
    val query = SelectQuery()
    query.init()
    return query
}

fun main() {
    val sql = query {
        select("id", "name", "age")
        from("users")
        where("age > 21")
    }
    println(sql)
}

Output:

Kotlin
SELECT id, name, age FROM users WHERE age > 21

4. DSL Design Patterns

4.1 Builder Pattern

The builder pattern is a common design pattern used to construct complex objects step-by-step. In the context of DSLs, it provides a fluent API to build complex structures.

Example: Builder Pattern with DSL

Kotlin
class PersonBuilder {
    var name: String = ""
    var age: Int = 0

    fun build(): Person {
        return Person(name, age)
    }
}

class Person(val name: String, val age: Int)

fun person(init: PersonBuilder.() -> Unit): Person {
    val builder = PersonBuilder()
    builder.init()
    return builder.build()
}

fun main() {
    val person = person {
        name = "John Doe"
        age = 30
    }
    println("Person: ${person.name}, Age: ${person.age}")
}

Output:

Kotlin
Person: John Doe, Age: 30

4.2 Command Pattern

The command pattern encapsulates a request as an object, thereby allowing for parameterization and queuing of requests.

Example: Command Pattern with DSL

Kotlin
interface Command {
    fun execute()
}

class PrintCommand(val message: String) : Command {
    override fun execute() {
        println(message)
    }
}

class CommandInvoker {
    private val commands = mutableListOf<Command>()

    fun addCommand(command: Command) {
        commands.add(command)
    }

    fun execute() {
        commands.forEach { it.execute() }
    }
}

fun commands(init: CommandInvoker.() -> Unit): CommandInvoker {
    val invoker = CommandInvoker()
    invoker.init()
    return invoker
}

fun main() {
    val invoker = commands {
        addCommand(PrintCommand("Hello"))
        addCommand(PrintCommand("World"))
    }
    invoker.execute()
}

Output:

Kotlin
Hello
World

4.3 Interpreter Pattern

The interpreter pattern defines a representation for a language’s grammar and provides an interpreter to evaluate sentences in the language.

Example: Interpreter Pattern with DSL

Kotlin
interface Expression {
    fun interpret(): Int
}

class Number(private val number: Int) : Expression {
    override fun interpret() = number
}

class Add(private val left: Expression, private val right: Expression) : Expression {
    override fun interpret() = left.interpret() + right.interpret()
}

class Subtract(private val left: Expression, private val right: Expression) : Expression {
    override fun interpret() = left.interpret() - right.interpret()
}

fun main() {
    val expression = Add(Number(5), Subtract(Number(10), Number(3)))
    val result = expression.interpret()
    println("Result: $result")
}

Output:

Kotlin
Result: 12

Conclusion

Kotlin's rich language features make it an excellent choice for building DSLs. By leveraging extension functions, type-safe builders, and various design patterns, you can create expressive and efficient DSLs tailored to specific domains. Whether it's for generating HTML, constructing SQL queries, or defining custom domain-specific tasks, Kotlin provides the tools and flexibility needed to build robust DSLs that enhance both productivity and code readability.