Hi, I’d like to present my book Creative DSLs in Kotlin. I’m not sure why the eBook isn’t on Amazon yet, but you can order it here.
I decided to write this book because I got frustrated with the toy examples, the lack of practical advise and the very limited variety of DSLs in other publications. The book is only ~200 pages long, I tried to avoid the usual “fluff”, but to rely on the intelligence of my readers instead. This is the content:
Chapter 1, Introduction, defines what a DSL is, discusses the differences between internal and external DSLs, and provides a general overview of DSL usage and underlying design principles.
Chapter 2, Requirements Analysis, helps you define the requirements and expectations for a DSL design before implementing it. Jumping in without defining your goals can lead to a nasty surprise, as there are more things to consider than you might think.
Chapter 3, Writing a DSL, suggests a simple methodology for implementing a DSL. Its steps help you avoid tunnel vision, get out when you hit a wall, and iterate your way to success.
Chapter 4, Relevant Language Features, takes a closer look at the Kotlin language from the perspective of a DSL designer. Even if you are very familiar with Kotlin, you may have never used some of the more obscure language features, such as extension properties.
Chapter 5, Algebraic DSLs, introduces the first category of DSLs, which support number-like behavior using operator overloading and infix functions. This is a fairly straightforward type of DSL, but mastering it will be useful in many other contexts.
Chapter 6, Builder Pattern DSLs and Method Chaining, covers the well-known builder pattern that you probably already know from Java, and shows some variations that attempt to make builders safer to use.
Chapter 7, Loan Pattern DSLs, is about using extension functions and the Loan Pattern to solve similar problems as the Builder Pattern DSL, but in a more idiomatic and convenient way.
Chapter 8, Modeling State Transitions in DSLs, presents various methods for modeling the stages or states of elements in your DSL. This is particularly useful when constructing objects in stages or modeling a finite state machine. The chapter explores alternatives to the simplistic “one state, one class” approach.
Chapter 9, String-Parsing DSLs, gives an overview of DSLs embedded in strings, which can be considered an intermediate case between internal and external DSLs. The chapter, for educational purposes, presents a manually written parser as well as demonstrating how the same parser can be written using a parser-combinator library.
Chapter 10, Annotation-based DSLs, explores the application of annotations for a DSL, which adds new meaning or behavior to existing code. The provided example code also illustrates how to use reflection to bring annotations to life.
Chapter 11, Hybrid DSLs, explains how to address scenarios where one of the previously discussed DSL categories is insufficient on its own and must be combined. Combining language features presents its own set of obstacles, and this chapter will equip you to overcome them.
Chapter 12, Code Generation for DSLs, discusses the benefits of code generation when manual DSL writing involves excessive boilerplate. At first, this task may seem daunting, but the example demonstrates that it is not something to be afraid of.
Chapter 13, Java Interoperability, provides an overview of potential issues when utilizing Kotlin DSLs within Java code, and offers practical solutions.
Chapter 14, Real-World DSL Examples, explores established and tested DSLs for valuable insight. In my view, small-scale examples do not provide sufficient guidance for quality DSL design. It is crucial to observe how DSLs tackle real-world problems, make compromises, and still provide a superior user experience.
Feel free to ask me anything. You can also message me a message at creativeDSLs@proton.me