13 DSL construction

 

This chapter covers

  • Building domain-specific languages
  • Using lambdas with receivers
  • Applying the invoke convention
  • Examples of existing Kotlin DSLs

In this chapter, we’ll discuss how you can design expressive and idiomatic APIs for your Kotlin classes through the use of domain-specific languages (DSLs). We’ll explore the differences between traditional and DSL-style APIs, and you’ll see how DSL-style APIs can be applied to a wide variety of practical problems in areas as diverse as database access, HTML generation, testing, writing build scripts, and many others.

Kotlin DSL design relies on many language features, two of which we haven’t yet fully explored. One of them you saw briefly in chapter 5: lambdas with receivers. They allow you to create a DSL structure by defining code-block-specific functions, properties, and behavior. The other is new: the invoke convention, which enables more flexibility in combining lambdas and property assignments in DSL code. We’ll study those features in detail in this chapter.

13.1 From APIs to DSLs: Creating expressive custom code structures

13.1.1 Domain-specific languages

13.1.2 Internal DSLs are seamlessly integrated into the rest of your program

13.1.3 The structure of DSLs

13.1.4 Building HTML with an internal DSL

13.2 Building structured APIs: Lambdas with receivers in DSLs

13.2.1 Lambdas with receivers and extension function types

13.2.2 Using lambdas with receivers in HTML builders

13.2.3 Kotlin builders: Enabling abstraction and reuse

13.3 More flexible block nesting with the invoke convention

13.3.1 The invoke convention: Objects callable as functions

13.3.2 The invoke convention in DSLs: Declaring dependencies in Gradle

13.4 Kotlin DSLs in practice

13.4.1 Chaining infix calls: The should function in test frameworks

Summary