Kotlin 2.1+ for Seniors: Stop Writing Code, Start Crafting it. 3 tips. Part 3.
Kotlin 2.1.0 introduces a host of experimental language features designed to help you write cleaner, more expressive code. In this post, we dive into three practical tips that leverage these new capabilities — features that are especially beneficial for seasoned coding professionals.
Tip 1: Simplify Loop Control with Non-Local Break and Continue
Loops nested within lambda expressions can be tricky. Until now, you could only perform non-local returns from inline functions — but Kotlin 2.1.0 changes that by adding non-local break and continue. This feature lets you exit or continue loops inside lambdas without resorting to awkward workarounds.
Consider a scenario where you want to skip processing certain items in a list:
fun processItems(items: List<Int>) {
items.forEach { item ->
if (item < 0) {
// Exit the current iteration non-locally without extra flags
continue
}
println("Processing $item")
}
}
To enable this feature, simply add the compiler option:
-Xnon-local-break-continue
It’s a game changer for reducing boilerplate and making your control flow more intuitive.
Tip 2: Streamline Conditionals with Guard Conditions in When
When expressions are already a powerful tool in Kotlin, but with the new guard conditions in when with a subject, you can now include additional criteria directly in your branch conditions. This not only flattens your code but also makes it much more readable.
For example, suppose you’re handling different animal types:
sealed interface Animal
data class Cat(val isHungry: Boolean) : Animal
data class Dog(val breed: String) : Animal
fun feedAnimal(animal: Animal) {
when (animal) {
is Dog -> println("Feed the dog")
is Cat if !animal.isHungry -> println("Cat is satisfied")
else -> println("Feed the animal")
}
}
This feature allows you to incorporate a condition (if !animal.isHungry
) directly within the branch, making your intent clear and concise. To try it out, enable it via:
-Xwhen-guards
Tip 3: Enhance String Handling with Multi-Dollar String Interpolation
Working with strings that contain literal dollar signs has always been a bit cumbersome in Kotlin. With multi-dollar string interpolation, you can now better control when interpolation occurs versus when you want to display literal dollar signs — ideal for templating or generating JSON schemas.
Here’s how it works:
val template = $$"""{
"price": "$${'$'}99.99",
"description": "Special offer: $${'$'}discount applied"
}"""
Conclusion
Kotlin 2.1.0’s experimental features aren’t just flashy new tricks — they’re practical tools for refining your codebase. By embracing non-local break/continue, guard conditions in when expressions, and multi-dollar string interpolation, you can reduce boilerplate, simplify control flow, and manage string templates with ease.
Stay ahead of the curve, experiment with these features, and contribute your feedback to help shape the future of Kotlin.
For more details on these features, check out the What’s New in Kotlin 2.1.0 documentation
Folow me for more :)