https://kotlinlang.org logo
m

mg6maciej

11/25/2015, 12:41 PM
About SAM, Java interop and interfaces: I know it's a decision that was explicitly made not to support Kotlin interfaces the same way Java interfaces are supported, but this makes us write a lot of boilerplate when creating stubs for Retrofit in tests, e.g.
Copy code
interface PasswordAuthApi {

    @GET("/oauth/v2/token?grant_type=password")
    fun call(
            @Query("username") email: String,
            @Query("password") password: String): Observable<AuthApiDto>
}
and in tests we have something like this:
Copy code
val authApiStub = object : PasswordAuthApi {
    override fun call(email: String, password: String): Observable<AuthApiDto> {
        return Observable.just(AuthApiDto("access_token", 4096, "refresh_token"))
    }
}
instead of simply something like:
Copy code
val authApiStub: PasswordAuthApi = { email, password ->
    Observable.just(AuthApiDto("access_token", 4096, "refresh_token"))
}
Also @voddan mentioned it earlier, I would be happy if this was implemented with
as
. This was actually the first thing I tried before I learned Kotlin interfaces are not supported. This is also how you do it in Groovy.
Copy code
val authApiStub = { email, password ->
    Observable.just(AuthApiDto("access_token", 4096, "refresh_token"))
} as PasswordAuthApi
I know I can simply create my Retrofit interfaces in Java as a workaround. I would prefer to have 100% code in one language tho.