2

I have the following function in scala, I intend to use it to convert any traversable into array and wrap any non-traversable with array:

  def asArray[@specialized(scala.Int, scala.Long, scala.Float, scala.Double/*, scala.AnyRef*/) T <: Any : ClassTag](obj: Any): Array[T] = obj match {
    case v: TraversableOnce[T] => v.toArray
    case v: Array[T] => v
    case v: T => Array[T](v)
    case _ => Array[T]()
  }

The @specialized annotation was added since to circumvent https://issues.scala-lang.org/browse/SI-6967. Namely a bug in scala 2.10 where primitive types may fail pattern matching.

However, the compiler gave me the following errors:

Error:(167, 27) type mismatch;
 found   : Any
 required: Double
    case v: T => Array[T](v)
                          ^

This is strange as v: T should be a double-type variable in the specialized implementation (instead of Any). Where did I do wrong?

tribbloid
  • 4,026
  • 14
  • 64
  • 103

0 Answers0