title: Why can't I use the interface as the generic type in this Rx transformer?
body: <p>I have the following data classes in Kotlin as reference:</p>
  
  <pre><code>interface GenericResponse {
  
      val error: Error?
  
      fun hasErrorObject(): Boolean = error != null
  }
  
  data class LoginResponse(
      val name: String,
      val auth: String,
      @SerializedName("error") override val error: Error? = null
  ) : GenericResponse
  </code></pre>
  
  <p><code>LoginResponse</code> implements <code>GenericResponse</code>. I'm using a Retrofit API in the following manner:</p>
  
  <pre><code>@POST("******")
  fun createSession(@Body body: LoginBody): Single&lt;Response&lt;LoginResponse&gt;&gt;
  </code></pre>
  
  <p>Furthermore, I use a transformer to extract the error from the <code>LoginResponse</code> if it exists as shown below:</p>
  
  <pre><code>class ExceptionTransformers {
      fun &lt;T&gt; wrapRetrofitExceptionSingle(): (Single&lt;Response&lt;T&gt;&gt;) -&gt; Single&lt;T&gt; {
          return { it: Single&lt;Response&lt;T&gt;&gt; -&gt;
              it.flatMap { response: Response&lt;T&gt; -&gt;
                  if (response.code() == 200 &amp;&amp; response.body() != null) {
                      val genericResponse = response.body() as GenericResponse
                      if (genericResponse.hasErrorObject()) {
                          Single.error { MyException(genericResponse.error!!) }
                      } else {
                          Single.just(response.body()!!)
                      }
                  } else {
                      Single.error { UnknownError() }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>As you can see, I have to cast the response body into a <code>GenericResponse</code> instance and then continue the login in the <code>ExceptionTransformer</code>. My question is this: Is there a way of using the return type as <code>GenericResponse</code> rather than type <code>T</code> (the generic)? I have tried to do that - but ran into a number of errors when using it like this:</p>
  
  <pre><code>fun createSession(loginBody: LoginBody): Single&lt;LoginResponse&gt; {
      return apiService.createSession(loginBody)
            .compose(exceptionTransformers.wrapRetrofitExceptionSingle())
  }
  </code></pre>