DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Why Kotlin Multiplatform is a Game-Changer for Startup Teams
  • Kotlin Code Style: Best Practices for Former Java Developers
  • Metal and the Simulated Annealing Algorithm
  • Reactive Kafka With Spring Boot

Trending

  • Fact-Checking LLM Outputs Programmatically: Building a Verification Layer That Catches Hallucinations
  • Stop Poisoning Your Models: How I Built a CV Dataset Quality Toolkit I Can Reuse Forever
  • Introduction to Retrieval Augmented Generation (RAG)
  • Building a DevOps-Ready Internal Developer Platform: A Hands-On Guide to Golden Paths, Self-Service, and Automated Delivery Pipelines
  1. DZone
  2. Coding
  3. Languages
  4. Learning Kotlin: How to Use Return When [Snippet]

Learning Kotlin: How to Use Return When [Snippet]

Let's take a look at a feature in Kotlin you might not know about: return when. In this post, we examine how to use it with some examples.

By 
Robert Maclean user avatar
Robert Maclean
·
Jul. 09, 18 · Code Snippet
Likes (3)
Comment
Save
Tweet
Share
22.9K Views

Join the DZone community and get the full member experience.

Join For Free

Continuing our break from the Koans, today, we are going to look at another cool trick I learned using Kotlin this week. We will also be focusing on the  when  keyword we learned about previously.

Let's start with a simple function to return the text for a value using when :

fun step1(number: Int):String {
    var result = "";
    when (number) {
        0 -> result = "Zero"
        1 -> result = "One"
        2 -> result = "Two"
    }

    return result;
}


We can avoid the next evolution by creating a variable and returning it directly (this is something I would do often in .NET):

fun step2(number: Int):String {
    when (number) {
        0 -> return "Zero"
        1 -> return "One"
        2 -> return "Two"
    }

    return ""
}


And, now, we get to the cool part — we can just return the when !

fun step3(number: Int):String {
    return when (number) {
        0 -> "Zero"
        1 -> "One"
        2 -> "Two"
        else -> ""
    }
}


Yup. The when  can return a value. This means that we can also do one final trick:

fun step4(number: Int):String = when (number) {
        0 -> "Zero"
        1 -> "One"
        2 -> "Two"
        else -> ""
    }


It is so cool that your logic can just return from a condition, and it works with if statements, too. You can even use this with the Elvis operator that we learned about previously:

fun ifDemo3(name:String?) = name ?: "Mysterious Stranger"
Kotlin (programming language)

Published at DZone with permission of Robert Maclean. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Why Kotlin Multiplatform is a Game-Changer for Startup Teams
  • Kotlin Code Style: Best Practices for Former Java Developers
  • Metal and the Simulated Annealing Algorithm
  • Reactive Kafka With Spring Boot

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook