[英]Kotlin vs. Java: Calling function with lambda parameter
I am having this function defined in a kotlin file 我在kotlin文件中定义了此功能
fun loadSubmissions(projectId: Long?, completion: (List<Submission>, Exception) -> Unit) { ... }
And want to call it from Java like this 并希望像这样从Java调用它
loadSubmissions(project.getProjectId(), (submissions, e) ->
{
updateSubmissions(submissions);
return null;
});
with 同
void updateSubmissions(List<Submission> submissionList)
{ .. }
But it gives me 但这给了我
Error:(226, 35) error: incompatible types: List<CAP#1> cannot be
converted to List<Submission>
where CAP#1 is a fresh type-variable:
CAP#1 extends Submission from capture of ? extends Submission
As I realize the parameter of the lambda function seems to be List<CAP#>
instead of the expected and defined List<Submission>
当我意识到lambda函数的参数似乎是
List<CAP#>
而不是预期的和定义的List<Submission>
If i convert the class to java, i can easily call that function with the lambda callback. 如果我将类转换为java,则可以使用lambda回调轻松调用该函数。
What am I doing wrong 我究竟做错了什么
You have not disclosed the type of variable submissions
, but the error message appears to indicate that it is List<? extends Submission>
您尚未公开变量
submissions
的类型,但是错误消息似乎表明它是List<? extends Submission>
List<? extends Submission>
. List<? extends Submission>
。 This is a different type from List<Submission>
, and references of the former type are not assignable to variables of the latter type (though the other way around is ok). 这是不同于
List<Submission>
类型,并且前一种类型的引用不能分配给后一种类型的变量(尽管相反也可以)。
That presents at least two problems for you: 这给您带来至少两个问题:
updateSubmissions()
, and updateSubmissions()
类型不正确,并且 loadSubmissions()
. loadSubmissions()
的第二个参数的类型不兼容。 To fix this, you need either to broaden the parameter types of those two methods or to narrow the type of variable submissions
. 要解决此问题,您需要扩大这两种方法的参数类型或缩小变量
submissions
的类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.