2016-06-14 9 views
2

私は2つのインターフェイスKotlin:トラブル理解ジェネリック

interface A 
interface B 
class Model() : A, B 

私は私のモデルクラスの一覧として一つのパラメータを渡す実装モデルを持っている、コンパイラはモデルAとBであることを理解しかし、私は2つのパラメータを渡すときそのうちの1つはT型(T:A、T:B)で与えられ、コンパイラはそれを理解できません。

protected fun <T> test(givenList: List<T>) where T : A, T : B { 

    val testList = ArrayList<Model>() 
    oneParamFunc(testList) // will compile 
    oneParamFunc(givenList) // will compile 

    twoParamFunc(givenList, testList) // won't compile (inferred type Any is not a subtype of A) 
    twoParamFunc<T>(givenList, testList) // won't compile (Required List<T>, Found ArrayList<Model>) 
} 

protected fun <T> oneParamFunc(list: List<T>) where T : A, T : B { } 
protected fun <T> twoParamFunc(oldList: List<T>, newList: List<T>) where T : A, T : B { } 

動作させるには何を変更する必要がありますか?

答えて

3

TModelは同じ種類ではない場合があります。そのため、リストパラメータごとに個別の汎用パラメータが必要になります。

fun <T1, T2> twoParamFunc(oldList: List<T1>, newList: List<T2>) 
     where T1 : A, T1 : B, T2 : A, T2 : B { } 
関連する問題