When I have a @specialized class, I retain the information about what primitive my type is. Is there a fast way to get a ClassTag[T]
, e.g. to create arrays?
This does not compile, because no ClassTag[T] is available
class Foo[@specialized T] {
def bar: Array[T] = new Array[T]
}
This works, but I want to avoid passing the ClassTag
class Foo[@specialized T: ClassTag] {
def bar: Array[T] = new Array[T]
}
This works, but is slow:
class Foo[@specialized T] {
def bar: Array[T] = new Array[T]
implicit def classTag: ClassTag[T] = {
val name = getClass.getName
if(name.endsWith("$I$sp") ClassTag.Int
else if(name.endsWith("$L$sp") ClassTag.Long
else ??? // you get the idea
}
}
Aucun commentaire:
Enregistrer un commentaire