Why are so few things @specialized in Scala's standard library?

后端 未结 3 868
夕颜
夕颜 2021-02-01 01:46

I\'ve searched for the use of @specialized in the source code of the standard library of Scala 2.8.1. It looks like only a handful of traits and classes use this an

3条回答
  •  一整个雨季
    2021-02-01 02:45

    Partial answer to my own question: I can wrap an array in an IndexedSeq like this:

    import scala.collection.immutable.IndexedSeq
    
    def arrayToIndexedSeq[@specialized(Int) T](array: Array[T]): IndexedSeq[T] = new IndexedSeq[T] {
      def apply(idx: Int): T = array(idx)
      def length: Int = array.length
    }
    

    (Ofcourse you could still modify the contents if you have access to the underlying array, but I would make sure that the array isn't passed to other parts of my program).

提交回复
热议问题