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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • The Window Closes: Why I Switched to Kotlin Before the Codebase Got Too Big
  • Testing Java and Spring Applications With Kotlin: Low-Risk to Adoption
  • Why Kotlin or Java Is the Wrong Question For Enterprises
  • Parallel Kafka Batch Processing With Kotlin Coroutines in Spring Boot

Trending

  • Evaluating RAG in Production — What the Numbers Actually Revealed
  • PWA on iOS, Part 2: HTTPS, Manifest and Service Worker
  • Running TypeSafe AI's Jev Inside Neo4j for Confidence-Aware Sentiment Decisions
  • Why AI Agents Need a Shared Enterprise Ontology

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Jul. 09, 18 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
23.2K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • The Window Closes: Why I Switched to Kotlin Before the Codebase Got Too Big
  • Testing Java and Spring Applications With Kotlin: Low-Risk to Adoption
  • Why Kotlin or Java Is the Wrong Question For Enterprises
  • Parallel Kafka Batch Processing With Kotlin Coroutines in Spring Boot

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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