From Java Builders to Kotlin DSLs

From Java Builders to Kotlin DSLs

Android, Java, Kotlin, Tutorial
From Java Builders to Kotlin DSLs Introduction DSLs - Domain Specific Languages - are an ever trending topic in Kotlin circles. They allow us to flex some of the most exciting language features while accomplishing more readable and maintainable solutions in our code. Today I'd like to show you how to implement a certain kind of DSL - we're going to be wrapping an existing Java Builder in Kotlin. No doubt you've come across the builder pattern in Java before, for example if you're an Android developer, you must've used an AlertDialog.Builder, an OkHttpClient.Builder, or a Retrofit.Builder at some point. Wrapping a builder like this is a good exercise in just pure DSL design. All you have to worry about is designing the API you provide with your wrapper, since…
Read More
The Power of Gradle Kotlin DSL

The Power of Gradle Kotlin DSL

Functional Programming, Gradle, Kotlin, Software Development, Tutorial
-The following is based on Gradle 4.3.1- A few weeks ago I started migrating most of my Groovy-based gradle.build scripts to Kotlin-backed gradle.build.kts scripts using the Kotlin DSL. Why would I do that? Kotlin is my language of choice and I love the idea of using a single language to do all my work. I never learned programming with Groovy and only know the bloody basics, which always makes me think: "This can't be the best way to do things...". Kotlin, on the other hand, is a language I use on a daily basis and therefore I know how to use the language appropriately. Additionally, Kotlin is a statically-typed language, whereas Groovy isn't. IDEs are having hard times offering code completion and error detection at compile time when a Groovy…
Read More
Web Applications with Kotlin ktor

Web Applications with Kotlin ktor

Functional Programming, Kotlin, Software Development, Web
Introduction Disclaimer: This ktor article was originally published in the Dzone Web Development Guide, which can be downloaded here. When Google made Kotlin an official language for Android a few months ago at Google I/O, the language gained much popularity in the Android world quickly. On the server side though, Kotlin is not as broadly adopted yet, and some people still seem to be cautious when backend services are involved. Other developers say that Kotlin is mature enough and we can safely use it for any server application in which Java could play a role otherwise. If you want to develop web apps with Kotlin, you can choose from various web frameworks like Spring MVC/WebFlux, Vert.x, Vaadin and basically everything available for the JVM. Besides the mentioned frameworks there's also…
Read More
Spring WebFlux with Kotlin – Reactive Web

Spring WebFlux with Kotlin – Reactive Web

Functional Programming, Kotlin, Software Development, Spring
Spring 5.0 - even fancier In this article I will show how Spring and Kotlin can be used together. If you’re not familiar with my recent articles, have a look at the other Kotlin related posts here. Besides Kotlin, I’ve always been interested in working with Spring ever since I started with Java back in 2011. I still like the framework although it’s getting bigger and bigger and you often don’t quite know which feature to choose amongst all the alternatives. As the framework itself is growing, the documentation, which is one of best you’ll ever get to see, also is. The thing I like most about Spring is that you can focus on your business logic from day one and don’t have much technical, infrastructural stuff to set up before…
Read More
Create a DSL in Kotlin

Create a DSL in Kotlin

Functional Programming, Java, Kotlin, Software Development
Kotlin as a programming language provides some very powerful features, which allow the creation of custom internal Domain Specific Languages (DSL).  One of these features, I also wrote about on this blog, is called Function Literals with Receiver, others are the invoke convention or infix notation. In this article, I will show how to create a Kotlin DSL by introducing a library that exposes a DSL as its API. I've often been struggling with Java’s API when I had to set up SSL/TLS connections in scenarios where I e.g. needed to implement HTTPS communication. I always felt like wanting to write a little library that can support me with this task, hiding away all the difficulties and of course the boilerplate needed for it. (more…)
Read More
Kotlin Operator Overloading – Working by Convention

Kotlin Operator Overloading – Working by Convention

Functional Programming, Kotlin, Software Development, Tutorial
Kotlin Operator Overloading and Conventions Introduction Kotlin supports a technique called conventions, everyone should be familiar with. For example, if you define a special method plus in your class, you can use the + operator by convention: Kotlin Operator Overloading. In this article, I want to show you which conventions you can use and I will also provide a few Kotlin code examples that demonstrate the concepts. (more…)
Read More
Kotlin Function Literals with Receiver – Quick Introduction

Kotlin Function Literals with Receiver – Quick Introduction

Functional Programming, Kotlin, Software Development
Kotlin Function Literals with Receiver - The Foundation for DSLs and many Library Functions Today I want to give a quick introduction to Kotlin Function Literals with Receiver. This concept is what makes Kotlin great for designing Domain Specific Languages as we know it from Groovy for example. Also, Kotlin's standard library includes many functions which many of you have already used or at least seen probably: apply and with are the most prominent ones. (more…)
Read More