Home » JVM Languages » Kotlin

Kotlin

Kotlin DSL: from Theory to Practice

I’ll try to explain the language syntax as simple as possible, however, the article still appeals to developers who consider Kotlin as a language for custom DSL building. At the end of the article I’ll mention Kotlin drawbacks worth taking into account. The presented code snippets are relevant for Kotlin version 1.2.0 and are available on GitHub. What is DSL? ...

Read More »

Kotlin and JUnit 5 @BeforeAll

Introduction In Kotlin, classes do not have static methods. A Java equivalent semantic can be provided to callers using the concept of a companion object though. This post will go into details of what it takes to support a JUnit 5 @BeforeAll and @AfterAll annotation which depend on the precense of a static methods in test classes. BeforeAll and AfterAll in Java Junit ...

Read More »

Testing Kotlin with Spock Part 1 – Object

The object keyword in Kotlin creates singleton in a very convenient way. It can be used for example as a state of an operation. Spock Framework is one of the most expressive and readable test framework available in the Java ecosystem. Let’s see how Kotlin object can be used in the Spock tests. What do we want to test? We ...

Read More »

Defending Public-By-Default in Kotlin

Some people have spoken against Kotlin’s decision to make classes, methods, etc. public by default (when no visibility modifier is used), and I would just like to pitch in on why I think JetBrains made the right decision on this one. Those who are against it bring up the principle of hiding everything that you can get away with in ...

Read More »

Kotlin – Reified type parameters sample

This post walks through a sample that demonstrates Kotlin’s ability to cleverly reify generic type parameters. So consider first a world where Kotlin does not support this feature, if we were using the Jackson library to convert a JSON to a Map with String based keys and Integer based values, I would use a code along these lines: @Test fun `sample ...

Read More »

Kotlin – Tuple type

It is very simple to write a Tuple type with the expressiveness of Kotlin. My objective expressed in tests is the following: 1. Be able to define a Tuple of up to 5 elements and be able to retrieve the elements using an index like placeholder, in a test expressed with 2 elements like this: val tup = Tuple("elem1", "elem2") assertThat(tup._1).isEqualTo("elem1") assertThat(tup._2).isEqualTo("elem2") ...

Read More »

Kotlin – Try type for functional exception handling

Scala has a Try type to functionally handle exceptions. I could get my head around using this type using the excellent Neophyte’s guide to Scala by Daniel Westheide. This post will replicate this type using Kotlin. Background Consider a simple function which takes two String, converts them to integer and then divides them(sample based on scaladoc of Try) : fun ...

Read More »

Kotlin for Java Developers: 10 Features You Will Love About Kotlin

Kotlin is a statically typed JVM language built by Jetbrains, the makers of the IntelliJ IDE. Kotlin is built upon Java and provides useful features such as null-safety, data classes, extensions, functional concepts, smart casts, operator overloading and more. This crash course into Kotlin for Java developers demonstrates the most important advantages that Kotlin has over Java and compares some of the ...

Read More »