Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Kotlin
Stage 5: Specialization

Part 16 / Topic 3 of 5

What's new in recent Kotlin releases

Kotlin ships a new minor (2.x) release every few months, and "what's new" lists get long fast. The table below only lists features big enough to matter day to day, each one actually compiled and run on this machine's kotlinc 2.4.21 before being written down here -- not quoted from memory of a changelog. The approximate "introduced around" version numbers are as published in Kotlin's own release notes; they were not re-verified against older compilers, since only 2.4.21 is installed here (see ../../TOOLING.md) -- but every snippet below ran, for real, on this machine.

Introduced around Feature What changed, in short
Kotlin 1.9 entries on enum class a cached List, replacing values()'s fresh array allocation on every call
Kotlin 1.9 data object a singleton object that also gets a data class-style toString()/equals()
Kotlin 2.1 guard conditions in when is Circle if isPremiumCustomer -> ...: a second condition on a branch, instead of nesting an if inside it
Kotlin 2.2 multi-dollar string templates $$"...$$name..." picks how many leading $ trigger interpolation, so a literal $name in a template no longer needs escaping
Kotlin 2.2 context parameters context(logger: Logger) fun f() { ... }: f can use logger without taking it as an ordinary parameter, as long as a Logger is in scope (via with(x) { f() }) where f is called
Kotlin 2.0 K2 compiler by default the new frontend/backend pipeline is what -language-version 2.0 and above means; kotlinc -X's own help text notes old-backend flags "have no effect when -language-version is 2.0 or higher"
enum class OrderStatus { PLACED, PACKED, SHIPPED, DELIVERED }

data object EmptyCart {
    fun describe() = "cart is empty"
}

sealed interface Shape
data class Circle(val radiusCm: Double) : Shape
data class Square(val sideCm: Double) : Shape

fun priceLabel(shape: Shape, isPremiumCustomer: Boolean): String = when (shape) {
    is Circle if isPremiumCustomer -> "premium circle, r=${shape.radiusCm}cm"
    is Circle -> "circle, r=${shape.radiusCm}cm"
    is Square if shape.sideCm > 50.0 -> "oversized square, side=${shape.sideCm}cm"
    is Square -> "square, side=${shape.sideCm}cm"
}

interface Logger { fun log(message: String) }

context(logger: Logger)
fun recordDelivery(orderId: String) {
    logger.log("delivered $orderId")
}
statuses: [PLACED, PACKED, SHIPPED, DELIVERED]
cart is empty
same instance: true
premium circle, r=10.0cm
circle, r=10.0cm
oversized square, side=60.0cm
usage: billcalc <amountInr>  (shows a result like 'Rs.999')
LOG: delivered ORD-7001

The context(logger: Logger) function above is called as recordDelivery("ORD-7001") -- no logger. anywhere in the call -- but only compiles inside a scope that has a Logger in context, like with(consoleLogger) { recordDelivery("ORD-7001") }. It compiled and ran with no opt-in flag needed on this kotlinc, which is its own small piece of evidence that it's no longer an experimental, flag-gated feature on this compiler version.

Full program: examples/03_whats_new_in_kotlin.kt.

Done here? The practice set for Part 16 — including the solutions — is on the part page.

Search the guide