Kotlin in 2022

About Me

John Burns

Staff Engineer @ GrubHub

CKUG Co-Organizer

twitter logo @wakingrufus

fediverse logo @wakingrufus@bigshoulders.city

github logo wakingrufus

GrubHub logo
  • Fully Remote and Hybrid Remote Roles
  • Unlimited PTO
  • 8-16 weeks of parental leave
  • 4.5 day work week

About You

First time at CKUG

Android

Backend

Other places

What's New?

  • Context Receivers
  • Definitely non-nullable types
  • Data Object
  • Generic Inline Classes
  • Compiler Improvements
  • Adoption

Context Receivers

  • 1.6.20
  • Implicit Parameters
  • Ext Fun with receiver, but MORE

Context Receivers


interface LoggingContext {
    val log: Logger // This context provides a reference to a logger
}

context(LoggingContext)
fun startBusinessOperation() {
    // You can access the log property
    log.info("Operation has started")
}

fun test(loggingContext: LoggingContext) {
    with(loggingContext) {
        // You need to have LoggingContext in a scope
        // to call startBusinessOperation()
        startBusinessOperation()
    }
}
                

Definitely non-nullable types

  • 1.6.20
  • Declare a generic type as non-nullable
  • Esp. useful in Java interop (platform types)
  • Uses type intersection

Definitely non-nullable types


fun <T> elvisLike(x: T, y: T): T = x ?: y
fun test(){
    elvisLike("", null) // valid
}
                

Definitely non-nullable types


fun <T : Any> elvisLike(x: T, y: T): T = x ?: y
fun test(){
    val s : String? = null
    elvisLike(s, "") // compile error at s
}
                

Definitely non-nullable types


fun <T> elvisLike(x: T, y: T & Any): T & Any = x ?: y
fun test(){
    val s : String? = null
    elvisLike(s, null) // compile error on null
}
                

Data Object

  • 1.7.20
  • Like data class, but for objects
  • Particularly good in sealed class hierarchies

Data Object


sealed class ReadResult {
    data class Number(val value: Int) : ReadResult()
    data class Text(val value: String) : ReadResult()
    data object EndOfFile : ReadResult()
}

fun main() {
    println(ReadResult.Number(1)) // Number(value=1)
    println(ReadResult.Text("Foo")) // Text(value=Foo)
    println(ReadResult.EndOfFile) // EndOfFile
}
                

Generic Inline Classes

  • 1.7.20
  • Inline classes can be generic now

Generic Inline Classes


@JvmInline
value class UserId<T>(val value: T)

fun printId(s: UserId<String>) {
    println(s.value)
}

fun printId(s: UserId<UUID>) {
    println(s.value.toString())
}

fun test(){
    printId(UserId(""))
    printId(UserId(UUID.randomUUID()))
}
                

Compiler improvements

  • Better Incremental Compilation in Gradle
  • Support for parallel compilation of a single module in the IR JVM backend
  • K2 Compiler has more features, still Alpha

Adoption

  • Kotlin continues to be dominant in the Android space (95% of apps)
  • Google announced that Kotlin a first-class backend JVM lang internally
  • Google did a study about mixing code
  • Mixing ecosystems (Java + C++) adds significant overhead
  • Mixing languages within ecosystem (JVM Java + JVM Kotlin) does not
  • Spring and Gradle continue to promote Kotlin as a preferred language
https://wakingrufus.github.io/kotlin-2022/

twitter logo @wakingrufus

fediverse logo @wakingrufus@mastodon.technology