2016-06-14 3 views
2

나는 두 개의 인터페이스코 틀린 : 문제 이해 제네릭

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

내 모델 클래스의 목록과 같은 하나 개의 매개 변수를 전달 구현하는 모델을 가지고, 컴파일러는 모델 A와 B는 것을 이해하지만 두 개의 매개 변수를 통과 할 때 그 중 하나는 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 { }