Programming DSLs in Kotlin
Design Expressive and Robust Special Purpose Code
by Venkat Subramaniam
Version: P1.0 (March 2021)
Copyright 2021 The Pragmatic Programmers, LLC. This book is licensed to the individual who purchased it. We don't copy-protect it because that would limit your ability to use it for your own purposes. Please don't break this trustyou can use this across all of your devices but please do not share this copy with other members of your team, with friends, or via file sharing services. Thanks.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC.
Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein.
About the Pragmatic Bookshelf
The Pragmatic Bookshelf is an agile publishing company. Were here because we want to improve the lives of developers. We do this by creating timely, practical titles, written by programmers for programmers.
Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://pragprog.com.
Our ebooks do not contain any Digital Restrictions Management, and have always been DRM-free. We pioneered the beta book concept, where you can purchase and read a book while its still being written, and provide feedback to the author to help make a better book for everyone. Free resources for all purchasers include source code downloads (if applicable), errata and discussion forums, all available on the book's home page at pragprog.com. Were here to make your life easier.
New Book Announcements
Want to keep up on our latest titles and announcements, and occasional special offers? Just create an account on pragprog.com (an email address and a password is all it takes) and select the checkbox to receive newsletters. You can also follow us on twitter as @pragprog.
About Ebook Formats
If you buy directly from pragprog.com, you get ebooks in all available formats for one price. You can synch your ebooks amongst all your devices (including iPhone/iPad, Android, laptops, etc.) via Dropbox. You get free updates for the life of the edition. And, of course, you can always come back and re-download your books when needed. Ebooks bought from the Amazon Kindle store are subject to Amazon's polices. Limitations in Amazon's file format may cause ebooks to display differently on different devices. For more information, please see our FAQ at pragprog.com/#about-ebooks. To learn more about this book and access the free resources, go to https://pragprog.com/book/vsdsl, the book's homepage.
Thanks for your continued support,
Andy Hunt
The Pragmatic Programmers
The team that produced this book includes: Dave Rankin (CEO) Janet Furlow (COO) Tammy Coron (Managing Editor) Series editor: Bruce A. Tate Potomac Indexing, LLC (Indexing) Gilson Graphics (Layout) Andy Hunt and Dave Thomas (Founders)
For customer support, please contact .
For international rights, please contact .
Table of Contents
Copyright 2021, The Pragmatic Bookshelf.
Acknowledgments
Were living in a rather unusual time, through a global pandemic combined with many regional issues. In hindsight, writing this book has given me a much needed reprieve, however temporary. The time spent on this book and interacting with Jackie, my editor, and the reviewers has been the high point of these months.
I would like to start by thanking Andy Hunt and Dave Rankin of the Pragmatic Bookshelf for giving me this opportunity. Dave was incredibly supportive and helpful in the formation of this book. His constructive feedback through every step was very useful.
Part of my motivation to write books is the opportunity to interact with Jackie, who, over time, has turned into a good friend. I admire her ability to put in such tireless efforts while dealing with many uncertainties of the current times. Thank you for spending time talking about the book and for motivating me when things appeared rather bleak.
This book has evolved so much based on the extensive feedback by an amazing set of technical reviewers. They offered so many suggestions, corrected me where the details were not accurate, and motivated me to rework parts that needed significant improvements. My sincere thanks to Tom Adam, Vasile Boris, Hadi Hariri, James Harmon, Rebecca Parsons, Eugene Petrenko, Ian Roughley, William Rudenmalm, Ilya Ryzhenkov, Giordano Scalzo, Ken Sipe, and Bruce Tate.
One benefit of releasing the book in beta is the opportunity to improve it using the feedback from early readers. I thank Graeme Collis and Curtis Riggs for helping me to fix errors.
I thank my wife Kavitha for her support and letting me hide in the basement hours on end as I worked on this book.
Copyright 2021, The Pragmatic Bookshelf.
Preface
Fluent, concise, expressive code, and low ceremony are some the traits of good domain-specific languages (DSLs). Creating a DSL is twice the joywe can write elegant code, which in turn can help our domain experts work with ease and convenience.
In this book youll learn how to design and implement your own DSLs using Kotlin. Ive 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. Its intended to quickly guide you through the steps necessary to build DSLs using Kotlin for JVM or Android applications.
If youre a programmer interested in using Kotlin to build DSLs, you picked the right book. Youll also find this book useful if youre 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.
Whats in This Book?
Weve 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 .
After a quick introduction of the steps to create DSLs with Kotlin, well dive straight into each of the steps. Well first focus on making the syntax fluent by exploiting the flexibilities of Kotlin. Then well look at ways to bring domain-specific vocabulary into your DSLs. Following that well devise ways to bring multiple contexts into the execution environments. Well then walk through the steps to make DSLs robust and resilient. Finally, well wrap with a few more examples of designing DSLs.
Each step of the way, well 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.