I have a java class file which works fine, but if I convert it to Kotlin it makes some problem. Here is a Java version
public class CallbackWrapper<T> implements Callback<T> {
private Wrapper<T> wrapper;
public CallbackWrapper(Wrapper<T> wrapper) {
this.wrapper = wrapper;
}
public void onFailure(Call<T> call, Throwable t) {
wrapper.onResult(t, null);
}
public void onResponse(Call<T> call, Response<T> response) {
wrapper.onResult(null, response);
}
public interface Wrapper<T> {
void onResult(@Nullable Throwable t, @Nullable Response<T> response);
}
}
And this is how I'm using this class without any problem...
request.enqueue(CallbackWrapper<RegisterResponse> { throwable, response ->
response?.let { callBack.onResponse(response.body() ?: RegisterResponse()) }
throwable?.let { callBack.onFailed(throwable.message!!) }
})
And here is Kotlin version after converting
class CallbackWrapper<T>(private val wrapper: CallbackWrapper.Wrapper<T>) : Callback<T> {
override fun onFailure(call: Call<T>, t: Throwable) {
wrapper.onResult(t, null)
}
override fun onResponse(call: Call<T>, response: Response<T>) {
wrapper.onResult(null, response)
}
interface Wrapper<T> {
fun onResult(t: Throwable?, response: Response<T>?)
}
}
The problem is that after converting it to Kotlin, code block where I'm using this class were not working. I don't understand what is the difference between them.
Here is compile error log
Error:(17, 63) Type mismatch: inferred type is (???, ???) -> [ERROR :<ERROR FUNCTION RETURN TYPE>] but CallbackWrapper.Wrapper<RegisterResponse> was expected
Error:(17, 65) Cannot infer a type for this parameter. Please specify it explicitly.
Error:(17, 76) Cannot infer a type for this parameter. Please specify it explicitly.