Question

I am not familiar with the Manifest type. The Jackson library uses this type, i.e. in ObjectMapper. So given this method:

def fromJson[T: Manifest](jsonInputStream: InputStream): T

I tried to call it with an anonymous type like:

....fromJson[{val numRounds: Int; val numSeconds: Int }](json)

but I got a compile error.

[error]  found   : scala.reflect.Manifest[Object]
[error]  required: Manifest[AnyRef{val numRounds: Int; val numSeconds: Int}]
[error] Note: Object >: AnyRef{val numRounds: Int; val numSeconds: Int}, but trait Manifest is invariant in type T.
[error] You may wish to investigate a wildcard type such as `_ >: AnyRef{val numRounds: Int; val numSeconds: Int}`. (SLS 3.2.10)
[error]           val d = jsonParser.fromJson[{val numRounds: Int; val numSeconds: Int}](json)

Can someone explain the problem here? Do I really have to create a proper class for the type?

Was it helpful?

Solution

I've answered your question in Manifest error for anonymous type :

Looks like it's not possible with manifests. You should use TypeTag instead. Like this:

import scala.reflect.runtime.universe._
object GenericSerializer
{
  def apply[T <:AnyRef]()(implicit tag: TypeTag[T]) = {}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top