私は、次のJavaコードを持っていた:Kotlinが次のラムダ引数を推論できないのはなぜですか(Java - > Kotlin変換後)?
public class DatabaseManager {
public interface Predicate<T> {
boolean test(T t);
}
private interface Consumer<T> {
void apply(T t);
}
private void updateLiveLists() {
updateLiveLists(liveList -> true);
}
private void updateLiveLists(Predicate<MutableLiveList<?>> predicate) {
forEachLiveList(liveList -> {
if (predicate.test(liveList)) {
refresh(liveList);
}
});
}
private void forEachLiveList(Consumer<MutableLiveList<?>> consumer) {
...
}
は、それから私は、Android StudioでJava -> Kotlin conversion
を使用:
class DatabaseManager {
interface Predicate<T> {
fun test(t: T): Boolean
}
private interface Consumer<T> {
fun apply(t: T)
}
private fun updateLiveLists(predicate: Predicate<MutableLiveList<*>> = { liveList -> true }) {
forEachLiveList({ liveList ->
if (predicate.test(liveList)) {
refresh(liveList)
}
})
}
private fun forEachLiveList(consumer: Consumer<MutableLiveList<*>>) {
...
}
は、次のエラーで失敗します。
Type mismatch
Required: DatabaseManager.Consumer<MutableLiveList<*>>
Found: (???) -> Unit
は、今私は、コードを変更しなければなりませんでした〜へ:
private fun updateLiveLists(predicate: Predicate<MutableLiveList<*>> = object : Predicate<MutableLiveList<*>> {
override fun test(t: MutableLiveList<*>): Boolean {
return true;
}
}) {
forEachLiveList(object : DatabaseManager.Consumer<MutableLiveList<*>> { // <--- !!
override fun apply(t: MutableLiveList<*>) {
if (predicate.test(t)) {
refresh(t)
}
}
})
}
私は明示的にこの匿名のインターフェイスを明示的にサブタイトルobject
として宣言しなければなりませんでした。なぜならKotlinはラムダを理解できませんでした。
fun refresh(vararg tables: Table) {
updateLiveLists({ liveList ->
for (table in tables) {
if (liveList.getTable() === table) {
[email protected] true
}
}
false
})
}
言う:それは助け場合
は、私はその下の関数で発生する同じ問題が持って
Type Mismatch:
Required: DatabaseManager.Predicate<MutableLiveList<*>>
Found: ??? -> Boolean
をそして私は、この代わりに
をしなければなりませんfun refresh(vararg tables: Table) {
updateLiveLists(object: DatabaseManager.Predicate<MutableLiveList<*>> { // <--
override fun test(t: MutableLiveList<*>): Boolean {
for (table in tables) {
if (t.getTable() === table) {
return true
}
}
return false
}
})
}
なぜ、これを避けるには? Kotlinがラムダタイプについて混乱することなく自分の述語/コンシューマを使用するにはどうすればよいですか? /u/lupajz
から
を露出+エイリアスを入力します私が今日見たように) –
私はここであなたは答えを見つけることができると思うhttps://stackoverflow.com/questions/43235423/converted-java-class-file-to-kotlin-makes-compilation-error –
@DimaKozhevinそれはちょっと長いです私は失敗が起こる2つの例を掲示した。 – EpicPandaForce