I have a relatively simple object with a method that makes an HTTP request. The request automatically extracts the data according to class type and spits out the necessary object.
The problem is that I can’t seem to reference the class dynamically. The code:
object abstractObject extends APIResource {
def retrieve(clazz: String, key: String) = {
request("GET", instanceURL(key)).extract[clazz]
}
}
clazz defines a type that is to be passed on to extract which allows the request to parse a JSON hash on-the-fly into an object. I need to somehow use that String to dynamically reference the class type and pass it to extract.
Any ideas or will I need to re-architect this?
Types only exist at compile time, so you can’t go from a run-time value to a type. Well, you can with Scala 2.10’s reflection, which would essentially get the code you need generated at run time, and then executed. You’d need to bundle both compiler and reflection jar files with the application.
However, none of that should be required for what you are proposing. A
ClassorManifestobject is enough — though, of course, maybe the API you are using doesn’t provide such alternative.