我有一个运行良好的Java类文件,但是如果将其转换为Kotlin,则会出现一些问题.这是Java版本
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!!) } })这是转换后的Kotlin版本
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>?) } }问题在于,将其转换为Kotlin后,我正在使用此类的代码块无法正常工作.我不明白它们之间有什么区别.
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.
这是编译错误日志
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.推荐答案
据我所知,您的代码有两个问题.
You have two issues with your code, as far as I can tell.
首先是Kotlin不允许使用lambda来实现Kotlin功能接口,因此您的lambda无法正常工作(它确实允许实现Java功能接口,因为Java没有适当的功能类型).因此,要使代码保持不变,您想使用对象符号来代替:
First is that Kotlin doesn't allow using lambdas to implement Kotlin functional interfaces, so your lambda isn't going to work (it does allow implementing Java functional interfaces, because Java doesn't have proper function types). So to call the code unchanged, you want to use object notation instead:
request.enqueue(CallbackWrapper<RegisterResponse>(object : CallbackWrapper.Wrapper<RegisterResponse> { override fun onResult(throwable: Throwable?, response: Response<RegisterResponse>?) { response?.let { callBack.onResponse(response.body() ?: RegisterResponse()) } throwable?.let { callBack.onFailure(throwable.message!!) } } }))如果您更喜欢使用lambda,则可以不同地声明CallbackWrapper类:
You can declare your CallbackWrapper class differently if you prefer using lambdas:
typealias Wrapper<T> = (t: Throwable?, response: Response<T>?) -> Unit class OtherCallbackWrapper<T>(val wrapper: Wrapper<T>) : Callback<T> { override fun onFailure(call: Call<T>, t: Throwable) = wrapper.invoke(t, null) override fun onResponse(call: Call<T>, response: Response<T>) = wrapper.invoke(null, response) }这就是but CallbackWrapper.Wrapper<RegisterResponse> was expected 要做的部分.
So that's the but CallbackWrapper.Wrapper<RegisterResponse> was expected part taken care of.
这仍然不起作用,这使我们知道错误消息的前半部分是inferred type is (???, ???) -> [ERROR :<ERROR FUNCTION RETURN TYPE>]的原因.这基本上是类型推断失败的原因.对callBack的调用似乎与CallbackWrapper暗示的CallBack接口不太匹配,我希望这可能与它有关.
This still won't work, though, which brings us to the reason the first half of the error message is inferred type is (???, ???) -> [ERROR :<ERROR FUNCTION RETURN TYPE>]. That's basically type inference failing. The calls to callBack don't quite seem to match the CallBack interface implied by CallbackWrapper, which I expect might have something to do with it.
使用更简洁的解决方案为您的翻新问题添加了答案.
Added an answer to your Retrofit question with a more concise solution.
更多推荐
将Java类文件转换为Kotlin会导致编译错误
发布评论