gpt4 book ai didi

java - Java 8 重载可以区分函数参数参数类型吗?

转载 作者:行者123 更新时间:2023-12-02 12:52:39 25 4
gpt4 key购买 nike

我正在将一个 kotlin 类注入(inject)到我的 java 代码中。 kotlin 类有两个签名几乎相同的方法:

fun <R> isFluxAuthorizedFor(
strings: List<StringRequest>,
then: (responses: List<StringResult>) -> Flux<R>
): Flux<R>


fun <R> isFluxAuthorizedFor(
string: StringRequest,
then: (per: StringResult) -> Flux<R>
): Flux<R> {

kotlin 类很好地支持这种重载。

但是,我很难让我的 IDE 使用正确的方法。我有一个匹配前者签名的方法:
private Flux<AuthorizedStrings> collectResults(List<StringResult> responses)
{
//not yet implemented
return null;
}

然而,当我尝试调用注入(inject)类的方法时,我得到了编译错误:
List<StringRequest> allStrings = new ArrayList<StringRequest>();
Flux<UserReadAuthorizations> test = authCheck.isFluxAuthorizedFor(allStrings, (it) -> this.collectResults(it) );

IDE 提出了两个建议:
"change type of 'it' to 'List<StringResult>'"

"change method 'collectResults(List<StringResult>)' to 'collectResults(StringResult)'"

这两者都意味着 Java(或至少编译器)无法确定我正在尝试调用其他方法。这是试图集成 Java 8 和 Kotlin 的问题吗? IDE 的一个怪癖? (我正在使用 Spring Tool Suite)一些愚蠢的用户错误,我还无法通过橡皮鸭解决?

最佳答案

我玩过你的代码,发现 IntelliJ 整体上绊倒了 lambda 的类型。我不得不投 it -> this.collectResults(it) Kotlin 期望的类型:

List<StringRequest> allStrings = new ArrayList<>();
Flux<UserReadAuthorizations> test = authCheck.isFluxAuthorizedFor(
allStrings,
(Function1<List<StringResult>, Flux<AuthorizedStrings>>) (it -> this.collectResults(it))
);

您的方法本身的签名不是问题。希望这对您在 STS 中也有帮助。

关于java - Java 8 重载可以区分函数参数参数类型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59381333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com