Error: private constructors in Java and object expressions


#1

Hey,

I updated my Kotlin code for Square Retrofit 2.0.0-SNAPSHOT (https://github.com/square/retrofit) and the compiler (Kotlin 0.12.613) started to complain.

Here is my attempt to reproduce this issue completely in Kotlin (http://try.kotlinlang.org/#/UserProjects/hl1mlli7si1md8pkkpnkartgs3/enu98juf78rprj5vhe5fuu3ea6):

data class Model

class Response<T> {

  private constructor() {}

}

interface Callback<T> {

  fun onResponse(response: Response<T>)

  fun onFailure(failure: Throwable)

}

fun main(args: Array<String>) {

  val callback = object : Callback<Model> {

  override fun onResponse(response: Response<Model>) {}

  override fun onFailure(failure: Throwable) {}

  }

}

This unfortunately did not reproduce the error, but might provide an insight about the problem. If I replace my Response in Kotlin with the Response class in Java (https://github.com/square/retrofit/blob/f15eee44ed/retrofit/src/main/java/retrofit/Response.java#L71) the compiler will output following error message:

'Response' is not an annotation class. Cannot access '<init>': it is 'private' in 'Response'. No value passed for parameter body. No value passed for parameter errorBody. No value passed for parameter rawResponse. A type annotation is required on a value parameter.

–Benjamin


#2

Oups, I'm so used to the "<type> <variable>" syntax of Java. I converted my code to "<variable>: <type>" and everything worked fine.


#3

But here is a question: Does Kotlin always add an extends-wildcard to generics?

Retrofit uses some reflection to build REST APIs. It complains with:

Exception in thread "main" java.lang.IllegalArgumentException: Method return type must not include a type variable or wildcard: retrofit.Call<java.util.List<? extends retrofit.Repo>>

I’ve defined my interface with

interface GitHubService {
    @GET("/users/{user}/repos?per_page=1")
    fun listRepos(@Path("user") user: String,
                  @Query("page") page: Int): Call<List<Repo>>
}

--Benjamin


#4

This was solved by replacing List with MutableList as suggested in https://youtrack.jetbrains.com/issue/KT-5792.