title: How NOT to return last expression's value from a Kotlin block?
body: <p>I have a method that returns nothing. Inside it, there's a try-catch block:</p>
  <pre><code>override fun sendResourceCreationRequest(...) {
      try {
          val requestEntity = ...
          restTemplate.exchange(requestEntity)
      } catch (e: RestClientException) {
          ...
      }
  }
  </code></pre>
  <p>Because the value of the last expression in a try block is returned implicitly, I get this compiler error:</p>
  <pre><code>Type mismatch.
  Required: Nothing
  Found: ResponseEntity&lt;???&gt;
  </code></pre>
  <p>I can assign the value to a variable and the error goes away:</p>
  <pre><code>val response: ResponseEntity&lt;Unit&gt; = restTemplate.exchange(requestEntity)
  </code></pre>
  <p>But now I have an unused variable which I (and lint) don't like. What's the best way to handle this?</p>
  <hr />
  <p><strong>Edit</strong></p>
  <p>The error went away when I specified the response type explicitly:</p>
  <pre><code>restTemplate.exchange&lt;Unit&gt;(requestEntity)
  </code></pre>
  <p>Perhaps something to do with <a href="https://github.com/spring-projects/spring-framework/blob/main/spring-web/src/main/kotlin/org/springframework/web/client/RestOperationsExtensions.kt#L289" rel="nofollow noreferrer">exchange</a> being an inline reified function or something?</p>
  <blockquote>
  <pre><code>@Throws(RestClientException::class)
  inline fun &lt;reified T&gt; RestOperations.exchange(requestEntity: RequestEntity&lt;*&gt;): ResponseEntity&lt;T&gt; =
        exchange(requestEntity, object : ParameterizedTypeReference&lt;T&gt;() {})
  </code></pre>
  </blockquote>