칼 리히터 :
나는 문제가 이해하는 이유가 있어요
class Main {
private val outputStreams: List<OutputStream>
@JvmOverloads constructor(outputStreams: List<OutputStream> = LinkedList()) {
if(outputStreams.contains(null)) {
throw IllegalArgumentException("outputStreams mustn't contain null")
}
this.outputStreams = outputStreams
}
}
컴파일 오류가 발생합니다 ...Main.kt:[12,26] Type inference failed. The value of the type parameter T should be mentioned in input types (argument types, receiver type or expected type). Try to specify it explicitly.
.
내가 사용하는 경우 outputStreams.contains(null as OutputStream))
컴파일은 성공하지만 Main(LinkedList<OutputStream>())
런타임에 실패로 인해
kotlin.TypeCastException: null cannot be cast to non-null type java.io.OutputStream
at kotlinn.collection.contains.nulll.check.Main.<init>(Main.kt:12)
at kotlinn.collection.contains.nulll.check.MainTest.testInit(MainTest.kt:13)
어느 것이 어느 가능한 한 원래의 자바에 가까운 코드를 유지하기위한 다른 접근 방식 잎 나를 내 의도뿐만 아니라 오히려 해결 방법을 찾고보다 같이이 문제를 이해.
s1m0nw1 :
컴파일러의 경우, 매개 변수는 outputStreams
포함 할 수 없습니다 null
그 유형이 될 때 List<OutputStream>
와 같은 반대 List<OutputStream?>
. 타입 시스템은 기대하지 않는 null
, 따라서 어떤을 확인해야이 목록, 내부로.
반면에 , 경우 해당 매개 변수가 실제로 포함 할 수있다 null
(이것은 자바 발신자에서 온다 때문에) 당신은 널 (NULL)을 명시 적으로 표시해야합니다 :List<OutputStream?>