문제

I have this method in package object:

def extractLoop[@specialized T](x: Map[T, T]) = {
    val whatever = x.head
    val stop = whatever._1
    def iteration(
            acc: Seq[T] = Seq(whatever._1, whatever._2),
            last: T = whatever._2): Seq[T] = {
        val next = x(last)
        if (next == stop) acc
        else iteration(acc :+ next, next)
    }
    iteration()
}

But I can't yet understand, why compiler (I have version 2.9.2) says type T is unused or used in non-specializable positions.?

도움이 되었습니까?

해결책

I believe the reason is simply that you are using a Map[T, T] which is not specialized.

Some illustration:

scala> class MyMap[A,B]
defined class MyMap
scala> def extractLoop[@specialized T](x: MyMap[T, T]) = {
     |   sys.error("TODO")
     | }
<console>:8: warning: type T is unused or used in non-specializable positions.
       def extractLoop[@specialized T](x: MyMap[T, T]) = {
           ^
extractLoop: [T](x: MyMap[T,T])Nothing

But if you specialize MyMap for its two type parameters, you have no warning:

scala> class MyMap[@specialized A,@specialized B]
defined class MyMap
scala> def extractLoop[@specialized T](x: MyMap[T, T]) = {
     |   sys.error("TODO")
     | }
extractLoop: [T](x: MyMap[T,T])Nothing
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top