Diving into Kotlin DSL Part-1: Getting started

Jigar Patel
Nov 26 · 3 min read
Unsplash: Abigail Lynn

Google announced Kotlin as first-class supported language for Android in July 2017. Since then many developers adopted Kotlin as their primary language for development and furthermore if we see the quote from Wikipedia, then

In 2018, Kotlin was the fastest growing language on Github with 2.6 times more developers compared to 2017.

Although Kotlin provides many features like null-safety, filters in collections, etc. Still, Code readability remains in discussion as all time trending. This question is not only limited to developers, but sometimes situations demand non-technical persons to operate the code base and it becomes difficult for them to either understand or work with it.

To solve this, we need to redefine how we write code in our day to day practices and there comes DSL in to the picture. Now you will wonder, what the heck is DSL?

Giphy: Steve Martin Idk

DSL stands for Domain Specific Language and the sole purpose of using it is to address common issues for particular domain and make code more readable. There are two types of DSLs:

  • Internal DSL: A language which has been developed and tightly coupled and are implemented in General Purpose Languages(GPL) like Kotlin or Java.
  • External DSL: A language parsed independently from the host GPL such like Regular Expressions, HTML, YAML etc.

Functions like extension, infix, higher order, literal with receiver, reified type parameters, etc plays a significant role in further deep down development of Kotlin DSL.

Extension Function

Often it happens that we want to write a function for particular class, but we do not have access to edit it or we do not want to add it as a member function. So, the mechanism through which the same function is available to call as if it is function/method of desired class is known as Extension function.

To declare an extension function, we need to prefix its name with receiver type, i.e. the type being extended.

This function can be called as per below:

Here, one of the benefits of using extension function is like without editing actual class, we are getting desired result and we can use this function within the scope of our code base.

Infix Function

When we follow standard coding practices, To perform any specific task we create functions and pass parameters inside parentheses. But does that goes with how we define it in real life scenario, like if somebody will ask us like: hey, is 5 divisible by 2? Surprisingly with infix we can write a function with similar pattern. So, let’s get more technical details on it.

Below are few basic requirements that we need to fulfill to create an infix function:

  • It must be a Member function or Extension function.
  • It must have a single parameter.
  • It must not accept a variable number of arguments and must have no default value.

Let’s create an Infix function.

One can call above function like this:

The main benefit here to use infix function is like if you give this syntax to any newbie as well, then they will be able to understand it easily.

Higher Order Function

When we create functions, often we need to pass a function as a parameter or we need a function as a return value, Guess what?!!! That’s higher order function. So to put it in a nutshell, we can call a function a Higher Order function if it does one of this, or both:

  • Allows a function as a parameter or
  • Returns a function

Pals, I will be posting series of posts sharing my knowledge on Kotlin DSL addressing various features. Till the time, If you have any question then feel free to ask in comments.

Mindful Engineering

Mindinventory Tech Blog

Jigar Patel

Written by

Mindful Engineering

Mindinventory Tech Blog

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade