Context bound [T: Manifest] -> Nothing
scala
Solution
Take a slightly simpler example:
def getManifest[A: Manifest] = implicitly[Manifest[A]]
Or the equivalent desugared version:
def getManifest[A](implicit whatever: Manifest[A]) = whatever
And then:
scala> def getIt[T]: Manifest[T] = getManifest
<console>:8: error: type mismatch;
found : Manifest[Nothing]
required: Manifest[T]
Note: Nothing <: T, but trait Manifest is invariant in type T.
You may wish to investigate a wildcard type such as `_ <: T`. (SLS 3.2.10)
def getIt[T]: Manifest[T] = getManifest
^
What's happening is that when the compiler tries to infer the type parameter for `getManifest`, there are lots of types that have `Manifest` instances in scope, and it has no way to choose between them, so it goes with its default choice, `Nothing`.
(You're seeing this error at runtime instead of compile-time because, well, that's what happens when you use reflection-based approaches to serialization.)
We can add a context bound:
def getIt[T: Manifest]: Manifest[T] = getManifest
Or, equivalently:
def getIt[T](implicit whatever: Manifest[T]): Manifest[T] = getManifest
Now everything is fine—there's a single most precise `Manifest` instance in scope, so the compiler can (appropriately) resolve the type parameter for `getManifest` to `T`.
You can see this a little more dramatically in the following example:
scala> trait Foo[A]
defined trait Foo
scala> def getFoo[A: Foo] = implicitly[Foo[A]]
getFoo: [A](implicit evidence$1: Foo[A])Foo[A]
scala> implicit object stringFoo extends Foo[String]
defined module stringFoo
scala> def getIt[T]: Foo[T] = getFoo
<console>:10: error: type mismatch;
found : Foo[String]
required: Foo[T]
def getIt[T]: Foo[T] = getFoo
^
Since there's only one `Foo` instance in scope (for `String`), the compiler can pick it when it's resolving the type parameter for `getFoo`, and we end up with a different type mismatch.
Problem
I use Jackson Scala Module. I've created a little serialization tool which handle the json payloads received by Play2 framework. ``` def unserializePayloadAs[T](implicit requestContext: RequestContext[JsValue]): T = { val json: String = Json.stringify(requestContext.request.body) unserialize(json) } def unserialize[T](json: String): T = { objectMapper.readValue(json) } ``` The readValue of Jackson Scala Module has the signature: ``` def readValue[T: Manifest](content: String): T = { readValue(content, constructType[T]) } ``` When trying to use my deserialization code, I have a stack. ``` Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate abstract type [simple type, class scala.runtime.Nothing$] (need to add/enable type information?) at [Source: java.io.StringReader@7bb78579; line: 1, column: 2] at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164) ~[jackson-databind-2.2.0.jar:2.2.0] at com.fasterxml.jackson.databind.deser.std.ThrowableDeserializer.deserializeFromObject(ThrowableDeserializer.java:77) ~[jackson-databind-2.2.0.jar:2.2.0] at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121) ~[jackson-databind-2.2.0.jar:2.2.0] at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888) ~[jackson-databind-2.2.0.jar:2.2.0] at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2041) ~[jackson-databind-2.2.0.jar:2.2.0] at com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper$class.readValue(ScalaObjectMapper.scala:157) ~[jackson-module-scala_2.10-2.2.0.jar:2.2.0] at utils.CustomSerializer$$anon$1.readValue(CustomSerializer.scala:17) ~[na:na] at utils.CustomSerializer$.unserialize(CustomSerializer.scala:39) ~[na:na] at utils.CustomSerializer$.unserializePayloadAs(CustomSerializer.scala:35) ~[na:na] ``` As expected, it works fine when I add the manifest in my code: ``` def unserializePayloadAs[T: Manifest](implicit requestContext: RequestContext[JsValue]): T = { val json: String = Json.stringify(requestContext.request.body) unserialize(json) } def unserialize[T: Manifest](json: String): T = { objectMapper.readValue(json) } ``` Can someone explain what happens there? When we call a method with a `Manifest` context bound, with a parametrized method with no `Manifest`, then the Manifest of `Nothing` is provided to the first method? I would have expected some kind of compilation error, telling me I'm calling the `readValue` with a parametrized type that has no `Manifest` or something like that, which seems more fail-fast.