Kotlin Reified Types in Inline Functions

Kotlin Reified Types in Inline Functions

Functional Programming, Kotlin, Software Development, Tutorial
Kotlin Reified Types in Inline Functions I've noticed that many people haven't ever heard of reified types or have problems understanding what they are and what they do. Therefore this little post is intended to bring some light into the darkness of Kotlin's reified types. Starting situation fun <T> myGenericFun(c: Class<T>) In an ordinary generic function like myGenericFun, you can't access the type T because it is, like in Java, erased at runtime and thus only available at compile time. Therefore, if you want to use the generic type as a normal Class in the function body you need to explicitly pass the class as a parameter like the parameter c in the above example. That's correct and works fine but makes it a bit unsightly for the caller. Inlined…
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 Coroutines Guide – Concurrent Programming in Kotlin

Kotlin Coroutines Guide – Concurrent Programming in Kotlin

Functional Programming, Java, Kotlin
updated 10/29/2018 Introduction and Motivation In this article, you will learn about Kotlin Coroutines: What they are, what they look like, and how they work. The demonstrated code examples were tested with Kotlin 1.3.0 and kotlinx.coroutines 1.0.0. Kotlin coroutines are one of the "bigger features" as indicated by the following quote, taken from JetBrains' blog: We all know that blocking is bad under a high load, that polling is a no-go, and the world is becoming more and more push-based and asynchronous. Many languages (starting with C# in 2012) support asynchronous programming through dedicated language constructs such as async/await keywords. In Kotlin, we generalized this concept so that libraries can define their versions of such constructs, and async is not a keyword, but merely a function. This design allows the…
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
Kotlin on the JVM – Bytecode Generation

Kotlin on the JVM – Bytecode Generation

Functional Programming, Kotlin, Software Development
Kotlin on the JVM - How can it provide so many features? Introduction What exactly is a "JVM language", what is Kotlin? Isn’t only Java meant to run on the JVM? Kotlin provides many features that aren’t available in Java such as proper function types, extension functions and data classes. How is this even possible? I’ve taken a deeper look at how Kotlin works under the hood and what "JVM language" actually means. We'll be having a look at Kotlin's bytecode generation. If you also thought about these things already, this article should bring some light into the darkness :) For a more detailed introduction to Kotlin’s features you can have a look at my Getting Started Guide. (more…)
Read More
Kotlin Sealed Class – An Explanation

Kotlin Sealed Class – An Explanation

Kotlin, Software Development, Uncategorized
Today I came across the Kotlin Sealed Class which I had never heard of before. After some research, I found that this concept is nothing new and is also available in Scala for example. So, yet another Scala feature JetBrains considered relevant and suitable for Kotlin? I like that :) Read this post if you're interested in more Kotlin features. Actually, this is a quite simple feature, which I'm going to explain in the following. Kotlin Sealed Class - Feature Explanation A sealed class can be subclassed and may include abstract methods, which means that sealed classes are abstract implicitly, although the documentation doesn't clearly say so. To actually make a class "sealed" we have to put the sealed modifier before its name, as we can see here: sealed class MyClass Restriction The…
Read More
Kotlin Generics and Variance (Compared to Java)

Kotlin Generics and Variance (Compared to Java)

Java, Kotlin, Software Development
This article covers the concepts of Generics and Variance in Kotlin and compares it to Java. Kotlin Generics differ from Java's in how users can define how they behave in sub-typing relationships. As opposed to Java, Kotlin allows defining variance on declaration-site, whereas Java only knows use-site variance. Kotlin Generics - What is Variance? Many programming languages support the concept of sub-typing, which allows implementing hierarchies that represent relationships like "A cat IS-an animal".  In Java, we can either use the extends keyword to change/expand behavior of an existing class (inheritance) or use implements to provide implementations for an interface.  According to Liskov’s substitution principle, every instance of a class A can be substituted by instances of its subtype B. The word variance, often referred to in mathematics as well,…
Read More
Setup Vert.x Application written in Kotlin with Gradle – Kotlin Reactive Programming

Setup Vert.x Application written in Kotlin with Gradle – Kotlin Reactive Programming

Java, Kotlin, Software Development
I decided to write a Vert.x application in combination with Kotlin in a simple example because I’m really interested in Reactive Programming and love to use Kotlin. In this post, I will give some basic information on Vert.x as a tool set for writing reactive applications on the JVM and also introduce Kotlin a bit. In the end, I want to demonstrate how this application can be set up in Gradle. (more…)
Read More