Multiline strings are very useful. But sometimes we want use the multiline string without the leading spaces that are added because of code formatting. To remove leading spaces we can use the trimIndent method. This method will find the least amount of leading spaces and removes that amount of spaces from each line. Also a first and last empty line are removed.
If we want a bit more control we can also add a character to the start of each line to show where the line starts. And then we use the method trimMargin and all spaces before that character are removed. The default character is the pipe symbol, |, but we can also define our own and pass it as argument to the trimMargin method.
Continue reading →
If we want to transform items in a collection we can use the map method. If we also want to use the index of the element in the collection in the transformation we must use the mapIndexed method. We must provide a lambda function with 2 arguments, where the first argument is the index of the element in the collection and the second argument is the element in the collection.
Continue reading →
Kotlin adds a lot of useful extensions to the collection classes. One of them is the indices property. The indices property returns the indices of the elements in the collection as an IntRange.
Continue reading →
Context with receivers is a new experimental Kotlin feature.
So let’s explore this feature a bit and see what it is all about.
Continue reading →
One of the great features of Kotlin is its interoperability with Java code. This allows you to easily call
'traditional' Java code from your Kotlin code, but it also helps you the other way around:
calling Kotlin code from Java.
Sometimes, a little extra work is needed to make some shiny Kotlin feature work with Java code. For example,
Kotlin supports default parameter values, which are not supported in Java. In this case, the @JvmOverloads annotation
can be used to generate overloads for functions that contain parameters with default values.
This annotation does not only work on functions, but can also be applied on constructors. In this post I will explain how
to use this feature on the primary constructor, as it might be confusing where to place the annotation.
Continue reading →
After completing the Kotlin for Java Developers Course on Coursera I was looking for an excuse to put my freshly gained Kotlin knowledge into action. I decided to address my frustration about the large amount of falsely detected movements by one of my security camera’s.
One of the core components of that solution is a REST API that receives an image and returns a list of detected objects. I decided to develop that using Kotlin, KotlinDL and KTor.
This blog posts describes the core components of the solution. The source code of the example is available at GitHub.
Continue reading →
Spring Security provides a lot of convenience to develop secure web applications.
However, it relies strongly on a SecurityContext stored in a thread-local (inside the SecurityContextHolder class).
If not mitigated, this causes issues in multi-threaded contexts. When using Kotlin Coroutines, there is an additional
abstraction layer where you don’t really know (and don’t want to know) on which thread(s) your code will be running.
Luckily, there is a relatively easy solution!
Continue reading →
How would you like to use your favorite backend language to develop frontend?
In this blogpost I’ll show you how to compile a small kotlin example to WebAssembly and how to run the output in your browser.
Continue reading →
When code evolves we usually deprecate old code.
Sometimes we come across deprecations without any hints with what to replace it with.
Kotlin has a solution for this by allowing you to specify a replace instruction.
For example, we created have an old REST client.
Continue reading →
Kotlin data classes and annotations go really well together, but it is easy to mess it up.
Continue reading →
When dealing with Maps in Kotlin, sometimes we’re only interested in entries for which the value is not null.
Although the Kotlin Standard Library contains a filterValues function that seems to be appropriate, this function
does not do any type conversions, resulting in a Map which won’t contain null values,
but is still a Map with values of a nullable type according to the compiler. There is a feature request
for the JetBrains team to add this functionality, but for now it has not been implemented (yet?).
Continue reading →
If you write lots of Kotlin code you might have noticed that it is annoying to write the named parameters when calling functions or when creating a new instance of class.
Continue reading →