kotlnにJavaメソッドを変換します。ラムダ式を返します。

ant2009:
kotlin 1.3.31

私はKotlinに変換しようとしていることをJavaで次のコードスニペットを持っています

private ArgumentMatcher<List<Person> customArgumentMatcher(final int size) {
    return argument -> argument.size() == size;
}

上記の私のundestandingは戻り型とインターフェース方法としてArgumentMatcherを有するメソッド宣言は、ラムダ式に実行され、得られたブール値が返されています。私は私の説明で間違っているなら、私を修正してください。

しかし、私が試してみて、これはKotlinに変換します

 private fun customArgumentMatcher(size: Int): ArgumentMatcher<List<Person>> {
    return { argument -> argument.size == size }
 }

私は次のエラーを取得します:

Required ArgumentMatcher<List<Person>>
found: (???) -> Boolean

任意の提案のための多くのおかげで、

なスロー:

以来は、ArgumentMatcherあなたが使用する必要があるのJava機能インタフェースです。

fun customArgumentMatcher(size: Int): ArgumentMatcher<List<Person>> {
    return ArgumentMatcher { argument -> argument.size == size }
}

参照してくださいSAM変換 Kotlin参照のセクションを。


また、使用することができます:

fun customArgumentMatcher(size: Int) = ArgumentMatcher<List<Person>> { it.size == size }

参照してくださいgidds'答えを上記の構文が必要な理由のいくつかの背景のために。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=231673&siteId=1