Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Discard return value to return Unit?

Tags:

kotlin

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.

like image 386
Dima Avatar asked Nov 19 '17 16:11

Dima


2 Answers

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()
}
like image 70
Grzegorz Adam Hankiewicz Avatar answered Nov 01 '22 21:11

Grzegorz Adam Hankiewicz


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()
    }
}
like image 40
hotkey Avatar answered Nov 01 '22 19:11

hotkey