I am trying to create a generic method for object updates using scala / java but I can’t get the class for a type parameter.
Here is my code:
object WorkUnitController extends Controller {
def updateObject[T](toUpdate: T, body: JsonObject){
val source = gson.fromJson(body, classOf[T]);
...
}
}
The error i get is
class type required but T found
I know in java you can’t do it but is this possible in scala at all?
Thanks!
Due Manifest is deprecated (Since Scala 2.10.0) this is the updated answer –
You should use
ClassTaginstead ofClassManifestand
.runtimeClassinstead of.erasureOriginal answer –
Yes, you can do that using manifests: