2

I have the following code:

trait MyTrait[B] {
  def func()
}

class A[B: ClassTag : Zero](val x: SparseVector[B]) extends MyTrait[B] {
  def this(x: Seq[(Int,B)]) = this({
    val (index, vals) = x.unzip
    vals.toArray
    new SparseVector[B](index.toArray, vals.toArray, vals.length)
  })

  def func(): Unit = {}
}

And get a error: No ClassTag available for B despite the fact that a ClassTag has been added to the generic parameter. What am I missing?

Above SparseVector is a member of the breeze linear algebra package (breeze.linalg)

user1094206
  • 920
  • 1
  • 12
  • 24

1 Answers1

2

the problem is that context bounds (which are just desugared as implicit parameters), aren't applied to auxiliary constructors. You'll need to explicitly (hah) declare the parameter list:

def this(x: Seq[(Int,B)])(implicit z: Zero[B], ct: ClassTag[B]) = ...

This feels like a bug in the compiler, but I could see it argued both ways.

dlwh
  • 2,257
  • 11
  • 23