title: Result nested twice when collecting a Flow in Kotlin 1.5 (kotlin.Result cannot be cast to `Type`)
body: <p>Recently I've updated my project to <strong>Kotlin 1.5</strong> and noticed some weird and unexpected behaviour when combining <code>Flow</code> with <code>Result</code>. Consider the following example:</p>
  <pre class="lang-kotlin prettyprint-override"><code>// Kotlin 1.5.0
  // Coroutines (kotlinx-coroutines-core) 1.5.0
  
  class Foo
  
  interface FooListener {
      fun onSuccess(foo: Foo)
      fun onFailure(error: Throwable?)
  }
  
  suspend fun collectData(listener: FooListener) {
      flow { emit(Result.success(Foo())) }
          .collect { result -&gt; // for reference
              when {
                  result.isSuccess -&gt; listener.onSuccess(result.getOrThrow())
                  result.isFailure -&gt; listener.onFailure(result.exceptionOrNull())
              }
          }
  }
  
  fun main() {
      runBlocking {
          collectData(object : FooListener {
              override fun onSuccess(foo: Foo) {
                  println(foo)
              }
  
              override fun onFailure(error: Throwable?) {
                  println(error)
              }
          })
      }
  }
  </code></pre>
  <p>The above compiles but fails in runtime with:</p>
  <pre><code>Exception in thread &quot;main&quot; java.lang.ClassCastException: kotlin.Result cannot be cast to Foo
  </code></pre>
  <p>After some debugging I noticed that the collected value (<code>result</code>) appears to be of type <code>Result&lt;Result&lt;Foo&gt;&gt;</code> instead of the expected <code>Result&lt;Foo&gt;</code>:</p>
  <pre class="lang-kotlin prettyprint-override"><code>suspend fun collectData(listener: FooListener) {
      flow { emit(Result.success(Foo())) }
          .collect { result -&gt;
              println(result) // Output: Success(Success(Foo@...))
              when {
                  result.isSuccess -&gt; listener.onSuccess(result.getOrThrow())
                  result.isFailure -&gt; listener.onFailure(result.exceptionOrNull())
              }
          }
  }
  </code></pre>
  <p>The problem disappears when I give up an idea of using externally provided listeners or callback functions in <code>collectData</code> and, for example, use something defined globally:</p>
  <pre class="lang-kotlin prettyprint-override"><code>class Foo
  
  interface FooListener {
      fun onSuccess(foo: Foo)
      fun onFailure(error: Throwable?)
  }
  
  val listener = object : FooListener {
      override fun onSuccess(foo: Foo) {
          println(foo)
      }
  
      override fun onFailure(error: Throwable?) {
          println(error)
      }
  }
  
  suspend fun collectData() {
      flow { emit(Result.success(Foo())) }
          .collect { result -&gt;
              println(result) // Output: Success(Foo@...)
              when {
                  result.isSuccess -&gt; listener.onSuccess(result.getOrThrow())
                  result.isFailure -&gt; listener.onFailure(result.exceptionOrNull())
              }
          }
  }
  
  fun main() {
      runBlocking {
          collectData()
      }
  }
  </code></pre>
  <p>The above runs without any errors.</p>
  <hr />
  <p>At this point it seems to me like a bug in the language, but I've been onto this for so long I'm not 100% sure and I could use a second opinion before bringing it up on Kotlin YouTrack.</p>
  <p>I've seen <a href="https://stackoverflow.com/q/66814634/11342519">Why does Kotlin crash passing generic Result parameter?</a> and <a href="https://youtrack.jetbrains.com/issue/KT-44837" rel="nofollow noreferrer">JVM / IR: ClassCastException with Result object when it is used by a generic method in a suspend call</a> but the bug not only looks the other way around, but it is supposed to be fixed in the newest version as well.</p>