dimanche 22 août 2021

scala pass T type to Java constructor

I am using Jackson read Yaml in scala 2.11/2.12 val mapper = new ObjectMapper(new YAMLFactory()), I think the Java constructor I am calling is

public <T> T readValue(String content, Class<T> valueType)

this code works

def load(): SomeClass {
  mapper.readValue[SomeClass](configStr, classOf[SomeClass])
}

But I want to change this class to T

mapper.readValue[T](configStr, classOf[T])

error class type required but T found

I searched some and changed it to

def load[T: ClassTag](): T = {
  mapper.readValue[T](configStr, classTag[T].runtimeClass)
}

But it says no construct methods are matched.





Aucun commentaire:

Enregistrer un commentaire