Kotlin

Have you moved to Kotlin yet? No, I don’t mean the island, I mean the language.

Getting Started

Kotlin:

Here’s a quick intro form Jeff at Fireship:

Let’s write and run a Hello World. For small Kotlin programs, you can write a simple script. Just write code at the top-level. The file extension must end in .kts:

hello.kts
println("Hello, world")

Run like this:

$ kotlin hello.kts
Hello, world

For more complex programs, put code in .kt files and compile and run. The program will begin at main:

triple.kt
fun main() {
    for (c in 1..40) {
        for (b in 1..c) {
            for (a in 1..b) {
                if (a * a + b * b == c * c) {
                    println("(%d, %d, %d)".format(a, b, c))
                }
            }
        }
    }
}
$ kotlinc triple.kt && kotlin TripleKt
(3, 4, 5)
(6, 8, 10)
(5, 12, 13)
(9, 12, 15)
(8, 15, 17)
(12, 16, 20)
(15, 20, 25)
(7, 24, 25)
(10, 24, 26)
(20, 21, 29)
(18, 24, 30)
(16, 30, 34)
(21, 28, 35)
(12, 35, 37)
(15, 36, 39)
(24, 32, 40)

You can of course run little Kotlin programs on replit or tio. For real-life work, most pros use IntelliJ IDEA.

There’s a REPL as well:

$ kotlin
>>> 5 * 8 % 2
res0: kotlin.Int = 0
>>> val dozen = 12
>>> dozen * 5
res2: kotlin.Int = 60
>>> "Hello".toLowerCase()
res3: kotlin.String = hello

Finally, a good to know: The official docs are really good!

Exercise: Familiarize yourself with the official docs. Browse them for an hour or so.

The Basics

Numbers and Booleans:

TODO

Strings:

TODO

How do we write our own classes? Not too unlike Java:

TODO

Kotlin has data classes similar to Java’s records. (Interesting to note Kotlin’s data classes were introduced years before Java’s records, so Java is the one playing catch up here.

TODO

require and check

These two functions help make writing secure constructors much more pleasant. require asserts that a condition holds by throwing an IllegalArgumentException if it does not, and check throws an IllegalStateException if its condition is false.

Kotlin also has value classes, which can be tagged inline:

TODO

Statements:

TODO

That’s it for the basics. The good stuff is coming soon.

From Java to Kotlin

People have had a love-hate relationship with Java since like forever. There have been tons of languages people have created to improve on Java and even run on Java’s JVM to interoperate with it. Prior to Kotlin, the most notable ones have been Groovy, Ceylon, and Scala. But Kotlin has had the most success.

Start by reading this article (“Nine Java Patterns That Kotlin Made Obsolete”) which focuses on the big advantages of Kotlin over Java, that is, those things that are clunky in Java but nicely cleaned up in Kotlin.

Next, try this video. It’s good because it uses Java 17 as the basis for comparison, so it’s more “fair”:

Data Classes

Null Safety

Extensions

Coroutines

Monads in Kotlin

Kotlin supports functional programming paradigms if you like them, and that means it directly supports a lot of the monadic programming patterns. Read this article for a nice walkthrough.

Summary

We’ve covered:

  • The language basics
  • Its relationship to Java
  • Data classes
  • Null safety
  • Extensions
  • Coroutines