Can't classOf[E] where E: ClassTag

705 views
Skip to first unread message

John Ky

unread,
Apr 30, 2013, 7:38:42 AM4/30/13
to scala-user
Any other way to get the Class object out?

class MyAsnListConverter[E: ClassTag] {
   def myClass = classOf[E] /// Error: Class type required by E found
}

Rafał Krzewski

unread,
Apr 30, 2013, 8:06:53 AM4/30/13
to scala...@googlegroups.com
def myClass = implicitly[ClassTag[E]].runtimeClass

Cheers,
Rafał

Simon Ochsenreither

unread,
Apr 30, 2013, 8:19:49 AM4/30/13
to scala...@googlegroups.com
I think it is a very good question why we make people go through implicitly[ClassTag[E]].runtimeClass, instead of making classOf work if a ClassTag is available.

Stefan Hoeck

unread,
Apr 30, 2013, 10:32:10 AM4/30/13
to scala...@googlegroups.com
Isn't there a distinction between the two? The return type of classOf[String] is Class[String] while for implicitly[ClassTag[String]].runtimeClass it is Class[_]. I actually have a use case where I'd need the former (Class[String]) but don't know how to get it from an implicit witness. Any ideas?

Cheers, Stefan

Am Dienstag, 30. April 2013 14:19:49 UTC+2 schrieb Simon Ochsenreither:
I think it is a very good question why we make people go through implicitly[ClassTag[E]].runtimeClass, instead of making classOf work if a ClassTag is available.This

Simon Ochsenreither

unread,
Apr 30, 2013, 10:50:05 AM4/30/13
to scala...@googlegroups.com
 
I actually have a use case where I'd need the former (Class[String]) but don't know how to get it from an implicit witness. Any ideas?

I don't think there is a way. It's like SomeClass.class vs. instanceOfSomeClass.getClass in Java. To get something like Class[String], the argument needs to be statically known (e. g. at compile-time).

I created a thread in scala-internals about this topic: https://groups.google.com/d/topic/scala-internals/-IFwlFk6Btc/discussion
Reply all
Reply to author
Forward
0 new messages