How to execute a Function within a When-statement? - kotlin

I am using the following when-statement in Kotlin:
when(name) {
"Sun" -> print("Sun is a Star")
"Moon" -> print("Moon is a Satellite")
"Earth" -> print("Earth is a planet")
}
And I have a function foo().
This function should be executed for every case of the when-statement.
I tried:
when(name) {
"Sun" -> print("Sun is a Star")
"Moon" -> print("Moon is a Satellite")
"Earth" -> print("Earth is a planet")
foo()
}
But I get an error.
How do I make a foo() function call within the when-statement?

If you really want to fire foo() from when, you can define a function, let's call it fooPrint() that prints the provided message and then invokes foo():
fun fooPrint(message: String) {
print(message)
foo()
}
And use this function with when:
when(name) {
"Sun" -> fooPrint("Sun is a Star")
"Moon" -> fooPrint("Moon is a Satellite")
"Earth" -> fooPrint("Earth is a planet")
}
Alternatively, as I've said in the comment, you can simply invoke foo() outside your when expression:
when(name) {
"Sun" -> print("Sun is a Star")
"Moon" -> print("Moon is a Satellite")
"Earth" -> print("Earth is a planet")
}
foo()

Question is if list of name is exhaustive. If it is, you can do like Alexander Ivanchenko suggests:
when(name) {
"Sun" -> print("Sun is a Star")
/** other code goes here */
}
foo()
Else:
when(name) {
"Sun" -> {
print("Sun is a Star")
foo()
}
/** other code goes here */
}

Related

Function that takes variable number of typed args and a closure with same number of typed args?

I'd like to make a function that takes a variable number of arguments of different types, and a closure, and call the closure with the same number of arguments, each corresponding to a type in the original argument list:
fun <A, B, ...>mergeWhenValid(
arg1: Either<Problem, A>,
arg2: Either<Problem, B>,
...,
closure: (A, B, ...) -> T
): Either<Problem, T> {
// do stuff and call closure(a, b, ...)
}
How might I accomplish this?
If your mergeWhenValid just returns closure result if all eithers are right and firstProblem.left() otherwise, you should use Either.fx<Problem, T> instead of your function. Example:
Either.fx<Problem, String> { "${eitherInt.bind()} ${eitherDouble.bind()} ${eitherFloat.bind()}" }
If your logic is more complex and you need somehow handle all eithers, you can do it either by creating special merging DSL:
fun <R> mergeWhenValid(block: MergeWhenValidScope.() -> R): R = MergeWhenValidScope().block()
class EitherProblem<out T>(internal val either: Either<Problem, T>)
class MergeWhenValidScope {
private val eithers = mutableListOf<Either<Problem, *>>()
operator fun <T> Either<Problem, T>.component1(): EitherProblem<T> {
eithers += this
return EitherProblem(this)
}
private fun doStuff(): Option<Problem> {
// you can use `eithers` here and choose one of their problems or create a new one
// if you return None, it will assume that all `eithers` are right,
// otherwise, problem will be wrapped in Either and returned
return eithers.asSequence().mapNotNull { it.swap().getOrElse { null } }.firstOption()
}
fun <R> combine(block: CombinerScope.() -> R): Either<Problem, R> =
doStuff().map { it.left() }.getOrElse { CombinerScope.block().right() }
object CombinerScope {
operator fun <T> EitherProblem<T>.invoke() = either.getOrHandle {
error("Unexpected problem $it")
}
}
}
Use case:
mergeWhenValid {
val (int) = eitherInt
val (double) = eitherDouble
val (float) = eitherFloat
combine { "${int()} ${double()} ${float()}" }
}
Or by pipelining functions which add all your eithers to some object:
fun <T> mergeWhenValid() = MergeWhenValidInit<T>()
class MergeWhenValidInit<T> {
operator fun <A> invoke(either: Either<Problem, A>): MergeWhenValid<A, T, T> =
MergeWhenValid(either, listOf(either)) { it }
}
class MergeWhenValid<A, B, C>(
private val either: Either<Problem, A>,
private val eithers: List<Either<Problem, *>>,
private val previous: (B) -> C // is allowed to be called only if all `eithers` are right
) {
private fun doStuff(): Option<Problem> {
// you can use `eithers` here and choose one of their problems or create a new one
// if you return None, it will assume that all `eithers` are right,
// otherwise, problem will be wrapped in Either and returned
return eithers.asSequence().mapNotNull { it.swap().getOrElse { null } }.firstOption()
}
operator fun invoke(block: (A) -> B): Either<Problem, C> =
doStuff().map { it.left() }.getOrElse { requireC(block).right() }
operator fun <D> invoke(either: Either<Problem, D>): MergeWhenValid<D, (A) -> B, C> =
MergeWhenValid(either, eithers + either) { next -> requireC(next) }
private fun requireC(next: (A) -> B): C = previous(next(either.getOrHandle {
error("Unexpected problem $it")
}))
}
Use case:
mergeWhenValid<String>()(eitherInt)(eitherDouble)(eitherFloat)() { float ->
{ double -> { int -> "$int $double $float" } }
}
Note: the last approach reverses the order of arguments and also forces you to write { c -> { b -> { a -> ... } } } instead of { c, b, a -> ... }.

Single-function listeners using lambda

With all the well-known single-function listeners we can use a simpler lambda notation
view.setOnClickListener { do() }
instead of the original, longer Java way of
view.setOnClickListener(object : View.OnClickListener {
override fun onClick(v: View?) {
do()
}
})
But what exactly makes this work? I tried to do the same with my own listener:
private var listener: OnCopyPasteClickListener? = null
interface OnCopyPasteClickListener {
fun onPasteClick(text: String)
}
fun setOnCopyPasteClickListener(onCopyPasteClickListener: OnCopyPasteClickListener) {
listener = onCopyPasteClickListener
}
and while the long approach works just fine:
copypaste.setOnCopyPasteClickListener(object : CopyPasteMenu.OnCopyPasteClickListener {
override fun onPasteClick(text: String) {
do(text)
}
})
I can't make it accept the short one:
copypaste.setOnCopyPasteClickListener {
do(it)
}
The IDE gives a type mismatch error.
Actually, if you have only one function to be invoked, I recommend you use Kotlin Callback.
typealias OnDoWorkListener = ((String) -> Unit)
class Work {
var doWork: OnDoWorkListener? = null
fun doSomething() {
doWork?.invoke("Message Here")
}
}
And in your function, you just set the callback to it
fun main() {
val work = Work()
work.doWork = {
Log.d("WORK", "This gets called from the `work` object. Message: $it")
}
work.doSomething();
}
We can also use function to set the listener as well.
class Work {
var doWork: OnDoWorkListener? = null
fun doSomething() {
doWork?.invoke("Message Here")
}
fun setOnWorkListener(listener: OnDoWorkListener) {
doWork = listener
}
}
fun main() {
val work = Work()
work.setOnWorkListener {
Log.d("WORK", "This gets called from the `work` object. Message: $it")
}
work.doSomething()
}
Higher order functions make this work:
Kotlin functions are first-class, which means that they can be stored
in variables and data structures, passed as arguments to and returned
from other higher-order functions. You can operate with functions in
any way that is possible for other non-function values.
From the same page:
Passing a lambda to the last parameter
In Kotlin, there is a convention that if the last parameter of a
function accepts a function, a lambda expression that is passed as the
corresponding argument can be placed outside the parentheses:
val product = items.fold(1) { acc, e -> acc * e }
If the lambda is the only argument to that call, the parentheses can
be omitted entirely:
run { println("...") }
Knowing this, a possible update on your class would look like:
class CopyPaste {
private var listener: (String) -> Unit = {}
fun setOnCopyPasteClickListener(onCopyPasteClickListener: (String) -> Unit) {
listener = onCopyPasteClickListener
}
fun doCopyPaste(value: String) {
listener.invoke(value)
}
}
fun main() {
val copyPaste = CopyPaste()
copyPaste.setOnCopyPasteClickListener { println(it) }
copyPaste.doCopyPaste("ClipboardContent!")
}
The class CopyPaste stores the listener, which is a function that takes a String parameter and does not return anything. Its function setOnCopyPasteClickListener accepts a function with the same signature as the listener property and at the end doCopyPaste accepts a String parameter and passes it to the stored function.
Actually, just after I posted, I searched for more thoughts and found this thread: https://youtrack.jetbrains.com/issue/KT-7770 This is indeed a debated limitation as it currently only applies to Java, not Kotlin itself. There is also a suggestion there that gives almost the required simplicity:
interface OnCopyPasteClickListener {
fun onPasteClick(text: String)
companion object {
inline operator fun invoke(crossinline op: (text: String) -> Unit) =
object : OnCopyPasteClickListener {
override fun onPasteClick(text: String) = op(text)
}
}
}
and then, thanks to this overloaded operator, it can be called as:
copypaste.setOnCopyPasteClickListener(CopyPasteMenu.OnCopyPasteClickListener { text ->
do(text)
})
But as the suggested answers offer a more idiomatic solution, I'll accept one of those, I only wanted to include this approach here for reference.

Discard return value to return Unit?

I am just starting with kotlin, so, forgive me if this is a basic question, I did do some googling, but that didn't turn up anything useful.
The question is how do I convert a value to Unit.
For example, in scala, if I write something like this:
def foo: Int = ???
def bar(x: String): Unit = x match {
case "error" => println("There was an error")
case _ => foo
}
The return type of the match expression is Any, but it is discarded by the compiler and Unit is returned by the function.
But doing something like this in kotlin:
fun bar(x: String): Unit = when(x) {
"error" -> println("There was an error")
else -> foo()
}
it complains about the foo part: inferred type is Int but Unit was expected
I know, that in this case, I can just get rid of the =, and put the body inside a block instead, that works, but I am looking for a general solution. What I was able to come with so far is just foo.let {}, but it seems kinda clumsy, especially if there are many cases like this where it needs to be done.
You can create an extension method on Any object and call it. I just prefer to use the name discard() rather than toUnit(), since I feel it conveys better the intent:
fun Any?.discard() = Unit
fun foo(): Int = 3
fun bar(x: String): Unit = when (x) {
"error" -> println("There was an error")
else -> foo().discard()
}
There's no way to do that out of the box, but you can make an extension function for this:
fun Any?.unit() = Unit
Then use it as:
fun bar(x: String): Unit = when(x) {
"error" -> println("There was an error")
else -> foo().unit()
}
Alternatively, make when a statement and not an expression:
fun bar(x: String) {
when(x) {
"error" -> println("There was an error")
else -> foo()
}
}
There are three solutions to your problem, which come to my mind:
Make when a statement instead of expression, so its result does not get returned:
fun bar(x: String) {
when(x) {
"error" -> println("There was an error")
else -> foo()
}
}
Use an extension to convert values to Unit:
fun Any?.asUnit() = Unit
Usage:
fun bar(x: String): Unit =
when (x) {
"error" -> println("There was an error")
else -> foo().asUnit()
}
Wrap the call into higher-order function call that returns Unit, e.g. with:
fun bar(x: String): Unit = with(x){
when (x) {
"error" -> println("There was an error")
else -> foo()
}
}

How to check method in when in kotlin

I wrote the code below.
fun check(list){ list.forEach{ when(it){
is Int -> something()
is String -> something()
//is method -> ???
else -> consume{it}
}}}
inline fun consume(f: () -> Unit){ f() }
But I don't want to check the method in else.
Is there any way to check the method in when?
As far as I know you can just omit the else -> consume{it} part. forEach function's signature is as follows:
inline fun <T> Iterable<T>.forEach(action: (T) -> Unit)
See action. It takes a function with Unit as output. This means that you do not need to return anything (Unit is Java's void equivalent).
So, in conclusion your code can look like this:
fun check(list: List<*>) {
list.forEach {
when (it) {
is Int -> something()
is String -> something()
//is method -> ???
}
}
}
I asked the question, but I combined the contents of the replies and what I dealt with, and I attached the results that I was trying to do.
var arr = arrayOf{"String", {method()}}
fun check(arr : Array<Any?>?){ arr.forEach{ when(it){
is Int -> println("int")
is String -> println("str")
else -> #Suppress("UNCHECKED_CAST") (it as () -> Unit)()
}}}
thanks

Is there a less ugly way to return function in Kotlin?

This declaration works, but is not the most beautiful code. Is there a way to return functions less ugly? I tried (s: String) -> writer.println(s) but this didn't work.
val writeStuff: (PrintWriter) -> (String) -> Unit = {
val writer = it
val f: (String) -> Unit = {
writer.println(it)
}
f
}
PrintWriter("test").use { writeStuff(it)("TEST") }
EDIT: a bit more concrete example:
val writeStuff: (PrintWriter) -> (String) -> Unit = { writer ->
{ writer.println(it) }
}
val sendStuff: (Any) -> (String) -> Unit = { sender ->
{ sender.equals(it) }
}
#Test fun test1() {
val li = listOf("a", "b", "c")
val process: List<(String) -> Unit> =
listOf(writeStuff(PrintWriter("a")), sendStuff(Object()))
process.map { li.map(it) }
}
First, you can simplify your code using lambda syntax with explicit parameter and inlining val f:
val writeStuff: (PrintWriter) -> (String) -> Unit = { writer ->
{ writer.println(it) }
}
But since Kotlin supports local function declarations, you can even make writeStuff a local fun instead of a val.
This would lead to the following code:
fun writeStuff(writer: PrintWriter): (String) -> Unit {
return { writer.println(it) }
}
Or, using the single expression syntax,
fun writeStuff(writer: PrintWriter): (String) -> Unit = { writer.println(it) }
The usage, however, will be the same:
PrintWriter("...").use { writeStuff(it)("...") }
I stumbled across this question while trying to figure out how to return a Function (the java interface) in Kotlin. While this doesn't directly answer the question, hopefully it'll help someone else who has the same query:
override fun myFun(param1: Object): Function<in Object, out String?> {
if (!param1.meetsCriteria())
return Function { obj -> null }
return Function { obj ->
"success"
}
}
In this case, I was overriding a method in a java interface that required me to return a Function instance. (Note that since the param is not used in my particular implementation above, I could remove it and just have the return result. eg return Function { null })
Edit: After some research, it turns out Kotlin covers this subject with their discussion on "SAM (single abstract method) conversions" here and here, though it may not be the most intuitive thing to look up when figuring out how to return Functions.