Design Expressive and Robust Special Purpose Code
Venkat Subramaniam
Kotlin#
DSLs#
Creating your own domain-specific languages (DSLs) is both challenging and exhilarating. DSLs give users a way to interact with your applications more effectively, and Kotlin is a fantastic language to serve as a host for internal DSLs, because it greatly reduces the pain and effort of design and development. But implementing DSLs on top of Kotlin requires understanding the key strengths of the language and knowing how to apply them appropriately. Learn to avoid the pitfalls and leverage the language while creating your own elegant, fluent, concise, and robust DSLs using Kotlin.
Internal DSLs remove the burdens of implementing a full blown language compiler. The host language quickly becomes your ally to creating DSLs, but the syntax you can choose for your DSLs is limited to what the host language allows. You can work around the limitations by tactfully bending the rules and exploiting the language capabilities. Learn the power of Kotlin and ways to design with it, in the context of crafting internal DSLs
Start by learning ways to exploit the flexibilities of Kotlin to make your DSLs fluent, expressive, and concise. Then pick up techniques to extend the language with domain specific properties and functions. Quickly move ahead to tie your DSL snippets into the runtime environment and context of execution of your applications. Design to prevent any non-sensical syntax in your DSL that may otherwise be valid in the host language. Finally, learn techniques to gracefully handle errors. Practice using the multiple examples that are included in each chapter.
Fire up your editor and follow along each example to become proficient in designing and implementing your own internal DSLs using Kotlin.
Table of Contents
1. Introduction
2. Exploit Fluency
3. Get Domain Specific
4. Provide Contexts
5. Manage Scope and Handle Errors
6. Practice with Examples
What You Need:
Kotlin version 1.3 or later and your favorite Kotlin IDE or code editor.
Fluent, concise, expressive code, and low ceremony are some the traits of good domain-specific languages (DSLs). Creating a DSL is twice the joy—we can write elegant code, which in turn can help our domain experts work with ease and convenience.
In this book you’ll learn how to design and implement your own DSLs using Kotlin. I’ve kept it really short so we can cut to the chase and get to implementing DSLs. Multiple examples will guide you through the essentials. Make sure to key in and execute each one of them to gain practice. Thank you for joining me on this journey.
Who Is This Book For?
This book assumes you are familiar with Kotlin and have some reasonable understanding of DSLs. It’s intended to quickly guide you through the steps necessary to build DSLs using Kotlin for JVM or Android applications.
If you’re a programmer interested in using Kotlin to build DSLs, you picked the right book. You’ll also find this book useful if you’re a lead developer, architect, or a technical manager who is involved in the development of applications that make use of DSLs.
You may also use this book to train your developers to become proficient with using Kotlin for programming DSLs.
What’s in This Book?
We’ve intentionally kept this book short to help you to quickly pick up key tips and tricks to create DSLs using Kotlin.
This book will not serve as an introduction to Kotlin nor to DSLs, as it assumes some knowledge of both. For an introduction to Kotlin refer to Programming Kotlin, and for an introduction to DSLs refer to Domain-Specific Languages.
After a quick introduction of the steps to create DSLs with Kotlin, we’ll dive straight into each of the steps. We’ll first focus on making the syntax fluent by exploiting the flexibilities of Kotlin. Then we’ll look at ways to bring domain-specific vocabulary into your DSLs. Following that we’ll devise ways to bring multiple contexts into the environments. We’ll then walk through the steps to make DSLs robust and resilient. Finally, we’ll wrap with a few more examples of designing DSLs.
Each step of the way, we’ll look at a number of code examples for you to practice along with. Take the time to key in each example, compile it, and execute it. Pause to make changes, break it, put it back together, try some variations, and then walk along to the next tip or trick to design your DSLs.