Pass function reference to 'when' statement?

Wouldn’t it be a nice feature to be able to pass a single-argument (probably) function reference to ‘when’ statement, and give the actual outcomes with specified function input in a statement body?
I mean, it would look something like this:

when (string.contains) { //here comes some magic syntax, idk really, but it would be really nice to have the implicit pass of our object to the function 
    "a" -> { doSomething() }
    "b" -> { doOtherthing() }
    else -> { doNothing() } 

IMHO, this should really apply only to ‘when’ keyword, i.e. be a ‘syntax sugar’ feature.
Why not?

1 Like

“Why not” is not how language design works. To make a proposal that can be seriously considered, you need to show a specific pattern that is hard to express using the existing language features in Kotlin, and show exactly how your proposal would improve that pattern. Syntax here is not something that can be waved away with an “idk”; it’s important that people reading code can understand what exactly is happening here.


I dont know the reason behind the question. But Kotlin when is already a lot fun that you can write all sort of fancy things in the expression.

       val string : String = "Kotlin"
        when  {
            string.contains("e") -> { println("You found e") }
            string.contains("K") -> { println("You found K") }
            else                 -> { println("You found nothing") }

The version I would like for more expressive when conditions would look something like this:

when(string)  {
    .contains("e") -> { println("You found e") }
    .contains("K") -> { println("You found K") }
    else                 -> { println("You found nothing") }

But that probably leads to parsing ambiguities. Perhaps that could be solved by allowing special case of it.xxxxx as condition as in:

when(string)  {
    it.contains("e") -> { println("You found e") }
    it.contains("K") -> { println("You found K") }
    else                 -> { println("You found nothing") }

If there are cases where you need it as a parameter to a method instead of the target that can be handled by wrapping in let as in:

it.let { someFunction(it) } -> doSomething ()

There are many cases where it would be nice to use parameterized version of when and you have to resort to more verbose version

1 Like

I would doubt if it’s really making programming more readable and easy to understand. The onething I see in scala is that it’s easy to write complex syntax but harder to understand. The feature I like in kotlin is ‘simple elegant way of programming’.

When expression is lightweight and easy to code compared to pattern matching in scala, and very superior to java’s switch.


Try this:

with(string) { when {
    contains("a") -> { doSomething() }
    contains("b") -> { doOtherthing() }
    else -> { doNothing() }

what about:
else -> {return}

1 Like

Maybe if we had it as

   ::testReferenceFunc -> action(string)
   else -> anotherAction(string)

Since references are already accepted and the when section just requires a statement returning a when felt this could work quite interestingly
the testReferenceFunc accepts the type provided in the when(string) section

1 Like