title: How to convert function output to Unit with Kotlin
body: <p>I have troubles with a function in Kotlin that should return Unit, but due to a usage of another function returning a Boolean, there is a type mismatch.</p>
  
  <p>Here is a contrived example:</p>
  
  <pre><code>fun printAndReturnTrue(bar: Int): Boolean {
      println(bar)
      return true
  }
  
  fun foo(bar: Int): Unit = when(bar) {
      0 -&gt; println("0")
      else -&gt; printAndReturnTrue(bar)
  }
  </code></pre>
  
  <p>Here, I actually do not care about the fact that <code>printAndReturnTrue</code> returns a boolean. I just want foo to perform side-effect operations. But the compiler warns about a type mismatch: my <code>else</code> should return a <code>Unit</code> value.</p>
  
  <p>Is there a nice way to convert a value to <code>Unit</code>?</p>
  
  <p>The simplest solutions I see are:</p>
  
  <pre><code>fun foo(bar: Int): Unit = when(bar) {
      0 -&gt; println("0")
      else -&gt; { 
          printAndReturnTrue(bar)
          Unit
      }
  }
  </code></pre>
  
  <p>or:</p>
  
  <pre><code>fun foo(bar: Int): Unit = when(bar) {
      0 -&gt; println("0")
      else -&gt; eraseReturnValue(printAndReturnTrue(bar))
  }
  
  fun eraseReturnValue(value: Any) = Unit
  </code></pre>
  
  <p>Or I use the full function form:</p>
  
  <pre><code>fun foo(bar: Int): Unit { 
      when(bar) {
          0 -&gt; println("0")
          else -&gt; printAndReturnTrue(bar)
      }
  }
  </code></pre>
  
  <p>I am sure there are some idiomatic ways to do that (or is is the last example?), but for now I did not find them.</p>