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?
“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 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.
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