Skip to main content
added sandbox demonstration of nearly identical code
Source Link
ecoe
  • 5.4k
  • 8
  • 62
  • 84

Vasil's and Maxim's answer helped me.

Personally, I prefer the syntax where implicit is used for adding such parameters (the presented : ClassTag is shorthand for it. So here, in case someone else also sees this to be a better way:

import scala.reflect.ClassTag object WorkUnitController extends Controller { def updateObject[T](toUpdate: T, body: JsonObject)(implicit tag: ClassTag[T]){ val source = gson.fromJson(body, tag.runtimeClass) ??? } } 

DisclaimerDemonstration: I did not compile the above, but my own similar code works this way.https://scastie.scala-lang.org/Vij5rpHNRDCPPG1WHo566g

Vasil's and Maxim's answer helped me.

Personally, I prefer the syntax where implicit is used for adding such parameters (the presented : ClassTag is shorthand for it. So here, in case someone else also sees this to be a better way:

import scala.reflect.ClassTag object WorkUnitController extends Controller { def updateObject[T](toUpdate: T, body: JsonObject)(implicit tag: ClassTag[T]){ val source = gson.fromJson(body, tag.runtimeClass) ??? } } 

Disclaimer: I did not compile the above, but my own similar code works this way.

Vasil's and Maxim's answer helped me.

Personally, I prefer the syntax where implicit is used for adding such parameters (the presented : ClassTag is shorthand for it. So here, in case someone else also sees this to be a better way:

import scala.reflect.ClassTag object WorkUnitController extends Controller { def updateObject[T](toUpdate: T, body: JsonObject)(implicit tag: ClassTag[T]){ val source = gson.fromJson(body, tag.runtimeClass) ??? } } 

Demonstration: https://scastie.scala-lang.org/Vij5rpHNRDCPPG1WHo566g

Source Link
akauppi
  • 18.2k
  • 15
  • 105
  • 131

Vasil's and Maxim's answer helped me.

Personally, I prefer the syntax where implicit is used for adding such parameters (the presented : ClassTag is shorthand for it. So here, in case someone else also sees this to be a better way:

import scala.reflect.ClassTag object WorkUnitController extends Controller { def updateObject[T](toUpdate: T, body: JsonObject)(implicit tag: ClassTag[T]){ val source = gson.fromJson(body, tag.runtimeClass) ??? } } 

Disclaimer: I did not compile the above, but my own similar code works this way.