Default Map in Kotlin

Default Map in Kotlin

Functional Programming, Kotlin, Software Development, Teaching, Tutorial
Does Kotlin have a default Map? Have you ever used a default map or default dict before? If you know Python a bit, you probably saw its defaultdict in action at some point. Kotlin also comes with a similar tool which I want to demonstrate in this little article. You can find the Python defaultdict documentation and some examples here but the basic use case is shown in the following snippet: from collections import defaultdict d = defaultdict(int) print(d["someKey"]) //0 The defaultdict can also be used with other types and makes sure that you don't get a KeyError when running your code. Instead, it provides a default value for unknown keys, which can be really helpful for grouping and counting algorithms like the following one: from collections import defaultdict data…
Read More
Diving into advanced Kotlin features

Diving into advanced Kotlin features

Kotlin, Teaching
Diving into advanced Kotlin features (Devoxx UK 2019) This year I gave my first "big conference" talk at Devoxx UK 2019 in London speaking about advanced Kotlin features. I targeted this talk towards an audience with intermediate Kotlin skills. Therefore I assumed that most of the standard language features, e.g., extension functions, class definitions, properties, and so on were familiar already, so I didn't explain everything during the session. The advanced Kotlin features I covered are listed next: Infix Functions Operator Overloading Higher Order Functions and Inling Lambdas with Receiver Domain Specific Languages Delegated Properties Generics and reified types (This topic had to be skipped due to time constraints) Inline Classes Contracts The talk went pretty well overall, although I had to sit during the entire session since the room…
Read More
Execute Kotlin Scripts with Gradle

Execute Kotlin Scripts with Gradle

Gradle, Kotlin, Software Development, Teaching, Tutorial
Organize Kotlin Scripts as Gradle tasks In this article, you will learn how you can organize multiple Kotlin scripts as Gradle tasks and make them easily executable this way. I've found a discussion about this here. Somebody wanted to execute Kotlin scripts with Gradle build scripts which is, of course, possible by using kotlinc as shown in this (Groovy) build script. This doesn't look very pretty though, and, as described in the corresponding thread, isn't very performant and manageable. Another solution would be to use Gradle scripts written with the Kotlin DSL and define custom tasks within a build.gradle.kts file, which obviously can hold and run Kotlin code naturally: // build.gradle.kts // // Execute Kotlin task with: gradle -q foo task("foo") { group = "com.kotlinexpertise" description = "my foo task"…
Read More
Android KTX – Android development with Kotlin

Android KTX – Android development with Kotlin

Android, Kotlin, Software Development, Teaching, Tutorial
Introduction Android KTX is an open source library or set of functionalities designed to make the Android development with Kotlin even more pleasant. You can find its website here. The abbreviation KTX stands for Kotlin Extensions, so this library is basically a set of extension functions, extension properties and other top-level functions. In this article, we take a look at what's inside this library and how we can take advantage of it. This library's goal is not to add new features to the existing Android APIs, but rather make those APIs easier to use by leveraging the features of the Kotlin language. Structure of Android KTX A very important thing to note at the beginning is that Android KTX provides functionalities which would be added to many individual projects by…
Read More
Kotlin Nullability Features

Kotlin Nullability Features

Functional Programming, Java, Kotlin, Software Development, Teaching, Tutorial
Null-Safe Programming - The Kotlin Way Disclaimer: This ktor article was originally published in the Dzone Java Guide 2018, which can be downloaded here. In this article, we will review the problems that may be caused by null pointers and how to avoid them in Java. After that, the article demonstrates how Kotlin nullability features work and how they improve your code. As Java developers, we're very accustomed to NullPointerExceptions (NPE) that are thrown at the runtime of an application. This almost always happens unintentionally in consequence of a bug, which is based on unrecognized references to null. The null reference is often used to indicate absent values, which isn't obvious to the programmer in many cases. Although Java relies on strong static typing, it doesn't let you distinguish between…
Read More
Why you should start contributing to StackOverflow

Why you should start contributing to StackOverflow

Kotlin, Software Development, Teaching, Tutorial
A little History A few years back, when I started getting into programming, I googled a lot of the problems I was facing during the day and mostly found my answers on StackOverflow. The place that every programmer is kind of dependent on. I still face problems and I still find my answers on StackOverflow. Something has changed, though. I don't only consume content on the website but also contribute to it. Until May 2017, I didn't even have an account on StackOverflow. At this time, I was getting interested in knowing how it's like to answer questions instead of just reading them. I enjoyed helping others to solve their Kotlin-related problems ever since I started learning the Kotlin programming language. This was my initial motivation for creating an account and…
Read More