런타임에서 유형 T에 대한 정보가 필요하지 않으므로 ClassTag가 여기서 쓸모 없다고 생각합니다.
함수에서 인수로 사용한 것과 동일한 유형을 반환하면됩니다.
class MyClass[T] extends A with B[T] with C with D
trait A
trait B[T] {
def usefulMethod: Unit = println("B")
}
trait C
trait D {
def usefulMethodToo: Unit = println("D")
}
나는 이와 같은 내 기능을 구현하려고 시도했지만 컴파일러는 Nothing
def helper[T, A <: B[T] with D](x: A): A = {
x.usefulMethod
x.usefulMethodToo
x
}
helper(new MyClass[Int])
어떻게 든 컴파일러에서 추론 할 수 있습니까?
문제는 추론 할 수 없다는 것입니다 T
.하지만이 경우 (과도하게 단순화 된 것일까 요?) 실제로 필요하지 않습니다.
def helper[A <: B[_] with D](x: A): A = {
x.usefulMethod
x.usefulMethodToo
x
}
이 해결 방법도 작동합니다.
def helper[T, A <: B[T] with D](x: A with B[T]): A = {
x.usefulMethod
x.usefulMethodToo
x
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다