Simple question, how does one get a TypeTag
from a classname?
So basically the TypeTag
equivalent of Class.forName
in Java.
Note: Manifest won't do for me here, I need a TypeTag. Although if there's a way to go from a manifest to a typetag, that would work too, as I can get a Manifest from the classname.
You can use
Class.forName
to get aClass
from aString
,ManifestFactory.classType
to get aManifest
from theClass
, and thenscala.reflect.runtime.universe.manifestToTypeTag
to get aTypeTag
:Manifests are deprecated and can probably disappear in future Scala versions. I don't think it is wise to rely on them. You can use only new Scala reflection to do what you want.
You can go from a string to a
Class
to get its class loader, and then it is possible to create aTypeTag
from aClass
through a mirror, as is outlined in this answer. Here is a slightly adapted piece of code demonstrating it: