我有一个Java类文件,它工作正常,但如果我将它转换为Kotlin它会造成一些问题。这里是一个Java版本转换java类文件为kotlin使编译错误
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);
}
}
这是我如何使用这个类没有任何问题......
request.enqueue(CallbackWrapper<RegisterResponse> { throwable, response ->
response?.let { callBack.onResponse(response.body() ?: RegisterResponse()) }
throwable?.let { callBack.onFailed(throwable.message!!) }
})
这里是科特林版本转换
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>?)
}
}
后问题在于,在将它转换为Kotlin之后,我使用这个类的代码块不起作用。我不明白他们之间有什么区别。
以下是编译错误日志
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.