将Java类文件转换为Kotlin会导致编译错误

编程入门 行业动态 更新时间:2024-10-20 00:47:20
本文介绍了将Java类文件转换为Kotlin会导致编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个运行良好的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会导致编译错误

本文发布于:2023-11-09 21:03:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573426.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   错误   文件   Java   Kotlin

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!