2017-10-13 6 views
1

私は、次の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から

+1

を露出+エイリアスを入力します私が今日見たように) –

+0

私はここであなたは答えを見つけることができると思うhttps://stackoverflow.com/questions/43235423/converted-java-class-file-to-kotlin-makes-compilation-error –

+2

@DimaKozhevinそれはちょっと長いです私は失敗が起こる2つの例を掲示した。 – EpicPandaForce

答えて

1

おかげで私は今の問題は基本的にhttps://discuss.kotlinlang.org/t/kotlin-and-sam-interface-with-two-parameters/293/5

のそれは

"why would you do it this way when you can use Kotlin's functional interfaces and type-aliases instead; if you need this then define the interfaces in Java".


に沸くのでKotlinに定義されたインタフェースは、SAM変換によって変換されない していることであることを知っています

いくつかの回避策があります:

1)私は問題の一部として上記で示したものであるインラインオブジェクト()

2)は、最も最短質問だオーバーロードされたメソッド

private typealias KotlinPredicate<T> = (T) -> Boolean; 

private typealias KotlinConsumer<T> = (T) -> Unit; 

class DatabaseManager { 
    private interface Consumer<T> { 
     fun apply(t : T) : Unit; 
    } 

    private fun forEachLiveList(consumer: Consumer<MutableLiveList<*>>) { 
     forEachLiveList({ 
      consumer.apply(it) 
     }) 
    } 

    private fun forEachLiveList(consumer: KotlinConsumer<MutableLiveList<*>>) { 
     ... 
    } 

interface Predicate<T> { 
    fun test(t : T) : Boolean; 
} 


fun updateLiveLists(predicate: Predicate<MutableLiveList<*>>) { 
    updateLiveLists({ 
     predicate.test(it) 
    }) 
} 

fun updateLiveLists(predicate: KotlinPredicate<MutableLiveList<*>> = { liveList -> true }) { 
    forEachLiveList({ liveList -> 
     if (predicate.invoke(liveList)) { 
      refresh(liveList) 
     } 
    }) 
} 
関連する問題