Kotlin DSL | Introduction

Glenn Sandoval
Kotlin and Kotlin for Android
5 min readJun 15, 2021

Introduction to Domain-Specific Language in Kotlin

In this series I will show you what a DSL is and how we can take advantage of Kotlin’s potential to create DSLs that allow us to write cleaner code. At the same time, you will discover the mechanism under the hood of many libraries that we include in our projects.

As a software developer, you probably have worked with Cascading Style Sheets (CSS), SQL queries, HTML, and if you are here, you probably are an Android Developer, so you have configured your projects with Gradle. What all these languages/technologies have in common is the fact that they use a specific language to solve some particular problem. This is what makes them be considered as Domain-Specific Languages which are meant to be effective solving that particular problem since their syntax is focused on being easily read, maintainable and sometimes they are even optimized as in the case of SQL. This is also what differentiates them from General Purpose (programming) Languages — GPLs — like Java, Phyton, Kotlin, etc.

How do we know if a certain language is a DSL or a GPL?

If this language/technology only works solving a particular problem and always in the same context, then you are in the presence of a DSL.

Let’s take the SQL case mentioned previously. With SQL we can only perform operations on a database. This language allows us to use its engine which executes queries efficiently. However, programming a mobile application or creating a web page are just impossible tasks. Another case is HTML, which allows us to structure web pages, and although they can be displayed on different devices, with HTML we couldn’t program a driver, create a library, or anything else different from structuring a web page.

So far, it seems like a DSL is a totally independent language that allows us to solve particular problems, however, having a DSL within a GPL is possible, and many libraries that we use are just that kind of DSL. A DSL doesn’t necessarily have to be an isolated and independent language. We can find a DSL dedicated to perform a specific task among a set of tasks performed by our application, coexisting with the rest of the code. This type of DSL is known as Internal DSL.

It is worth mentioning that an internal DSL coexists with the rest of the code with no conflicts because it is actually built on top of the same language. In fact, if we wrote regular code inside of DSL instructions, it would perfectly work.

An example of an internal DSL in Kotlin is the code that we write for dependency injection using Koin:

🔗 This code snippet has been taken from Koin’s official website

Another internal DSL example is the code that we write to create microservices or web applications using Ktor framework:

🔗 This code snippet has been taken from Ktor’s official website

You may notice a pattern in the structure and syntax on both code snippets. Basically, instead of following the usual sequential structure where an object is instantiated and its properties are set afterwards by calls to their respective setters, it does everything with nested curly braces where other objects are instantiated and their properties are set in such a way that each object’s composition and their intervention in each context are quite self-explanatory.

If you are an Android Developer, you probably have heard about the Jetpack Compose toolkit which, by the way, exploits Kotlin’s features by making use of its own DSL for some APIs like LazyRow and LazyColumn as they state on their official documentation.

🔗 This code snippet has been taken from Jetpack Compose’s official documentation.

Declaring a UI in such a clear and versatile way compared to layouts in XML, undoubtedly will revolutionize Android applications development as soon as Compose first stable version is released. Such clarity and versatility is obtained thanks to Kotlin’s potential as an hybrid paradigm programming language that allows us to create our own DSLs.

What do we need to create a DSL in Kotlin?

I’ve already mentioned some of the advantages that a DSL provides us, nevertheless, its creation comes at a high cost since a deeper knowledge of Kotlin is required, especially in its facet of functional programming language.

Assuming that you already have the knowledge regarding object-oriented programming, to create a DSL with Kotlin it is necessary that you additionally have the knowledge of at least the following:

In addition, to build a more idiomatic and even more efficient DSL, the following can also be applied:

Once you have learned how to apply all these previous features, you’ll be able to create a DSL to turn code that looks like this…

Snippet #1

…into code that looks like this…

Snippet #2

…getting the following output with both code snippets:

Output

Although both examples are easy to understand because of the simplicity of the codebase, you should take into account that we, as developers, deal with more extensive and complex code every day on real projects. Code like Snippet #1 becomes more difficult to read and maintain as it grows, while Snippet #2 will remain clear.

That’s all for now. In the next article I’ll show you all Kotlin’s features previously mentioned so you will be able to continue with the rest of articles where I’ll provide you with a codebase starting with Snippet #1 to turn it into Snippet #2 by building a DSL, step by step, on top of that codebase without changing a single line of it.

💬 If you enjoyed this article, you can show your appreciation by buying me a coffee at the link below. Thanks for reading and for your support.

--

--

Glenn Sandoval
Kotlin and Kotlin for Android

I’m a software developer who loves learning and making new things all the time. I especially like mobile technology.