Design for Separate Implicit Contexts

In the previous example, all the variables came in from a single context object—that is, a single receiver. In a more complex DSL, where many different groups of variables and functions are used, keeping everything in one context can get complex and hard to maintain. In this section we’ll see how different parts of a DSL may use different contexts.

Gradle kts build files are a great example of a Kotlin DSL—the syntax is lightweight, fluent, concise, and has just the essence. The DSL snippet in this section follows in the spirit of Gradle’s Kotlin DSL.

Suppose we want to express the configurations for building a project—the dependencies, the locations of the source code and test files, parameters to pass to ...

Get Programming DSLs in Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.