简体   繁体   中英

Scala - get Class for a specialized class

I need to get a Class object for a specialized class. eg

import breeze.linalg.DenseVector

val compileTimeClass = classOf[DenseVector[Double]]
println(compileTimeClass)

val denseVector = DenseVector(0.5, 1.0, 1.0, 0.5)
println(denseVector.getClass)

Output is:

class breeze.linalg.DenseVector
class breeze.linalg.DenseVector$mcD$sp

If I understand it correctly, Scala is creating specialized versions of DenseVector at runtime? This is an issue for me as I need to register one of these specialized classes for serialization with Kryo.

Other than calling getClass on a specialized instance of DenseVector, is there not a way to get a Class object of a specialized type?

Turns out that even though:

kryo.register(classOf[breeze.linalg.DenseVector$mcD$sp])

Shows as a compile time error in IntelliJ, it actually compiles and works as expected. It means we'll have to explicitly register every specialization that we use and serialize. The Twitter chill library gets round this for Tuple* classes by auto generating the registration and serialization code with https://github.com/twitter/chill/blob/develop/scripts/tuple_serializers.scala

Well... that will happen with every type-parametrized class. for example List

scala> classOf[List[Int]]
res8: Class[List[Int]] = class scala.collection.immutable.List

scala> val l = List[Int](1, 2, 3, 4)
l: List[Int] = List(1, 2, 3, 4)

scala> l.getClass
res9: Class[_ <: List[Int]] = class scala.collection.immutable.$colon$colon

Now coming back to your question, I think you just need to register DenseVector with kryo .

After that kryo should be able to deal with any DenseVector[A] , as long as that A is also registered with kryo.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM