Swift: Lift return statement out of expression

Hello guys,

I have a simple question about return statements in swift.

In Kotlin, you can do something like this:

fun foo(argument: String): String {
     return when (argument) {
           "huhu!" -> "nice"
           "hi" -> "..."
           else -> ""
     }
}
foo("hi") // will return "..."
/*
The kotlin "when" expression is similar to swift's switch,
notice the lifted-out return statement.
*/

For the same thing, in swift the way i knew is:

func foo(_ argument: String) -> String {
     switch argument {
     case "huhu!": return "nice"
     case "hi": return "..."
     default: return ""
     }
}
foo("hi") // will return "..."

So my question is, is there a way to also do the return-lift-out in swift?

Thank you for your answers!

~ Finder

Accepted Answer

AFAIK, that's not a case where we can omit return : https://www.swiftbysundell.com/questions/when-can-the-return-keyword-be-omitted/

You could post the suggestion to the Swift forum.

Swift: Lift return statement out of expression
 
 
Q