Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the classOf a ClassTag?

This doesn't work - is there any way I can keep my code generified and somehow reflect the classOf a ClassTag??

class Foo[T : reflect.ClassTag] { def foo = classOf[T] }

<console>:7: error: class type required but T found
   class Foo[T : reflect.ClassTag] { def foo = classOf[T] }

It seems this should work but alas :(

like image 845
JasonG Avatar asked May 31 '14 21:05

JasonG


People also ask

How do I join a ClassTag class?

ClassTag provides an option for parents to sign up for an account and join their child's class. Your child's teacher should have added either your email address or mobile number in their class directory. In order to join the class, there's a link on the email or SMS notification that you need to click on.

Is ClassTag completely free?

ClassTag connects teachers and families with one easy-to-use app for all their communication needs. And - it's free!


1 Answers

Yes, that's possible.

class Foo[T: reflect.ClassTag] {
  def ctag = implicitly[reflect.ClassTag[T]]
  def foo: Class[T] = ctag.runtimeClass.asInstanceOf[Class[T]]
}

or shorter:

class Foo[T](implicit ctag: reflect.ClassTag[T]) {
  def foo: Class[T] = ctag.runtimeClass.asInstanceOf[Class[T]]
}
like image 126
Klaus Avatar answered Sep 19 '22 06:09

Klaus