Scala: “No manifest available for type T”

2020-02-11 21:26发布

问题:

I am working on a Lift project with mixed Scala and Java code.

On the Java side, I have the following relevant items:

interface IEntity

interface IDAO<T extends IEntity> {
    void persist(T t);
}

On the Scala side, I have the following:

abstract class Binding[T <: IEntity] extends NgModel {
    def unbind: T
}

class BasicService[E <: IEntity](serviceName: String, dataAccessObject: IDAO[E]) {
      def render = renderIfNotAlreadyDefined(
        angular.module("myapp.services")
          .factory(serviceName,
            jsObjFactory()
              .jsonCall("persist", (binding: Binding[E]) => {  //<---COMPILATION ERROR
                  try {
                    dataAccessObject.persist(binding.unbind)
                    Empty
                   } catch {
                   case e: Exception => Failure(e.getMessage)
                   }
              })
          )
     )
}

This code will not compile. I get the following error at the point indicated above:

No Manifest available for Binding[E].

It is not clear at all to me why this occurs, but I am guessing it has something to do with this being a nested method invocation. The code compiles fine if I declare a member function with Binding[E] as a parameter, for example:

def someFunction(binding: Binding[E] = { // same code as above }

Why does this happen, and how can I work around it?

回答1:

Turns out this is relatively easily solved by implicitly passing on the manifest for the type in question, either in the constructor or the method itself:

class BasicService[E <: IEntity](serviceName: String, dataAccessObject: IDAO[E])(implicit m: Manifest[Binding[E]]) {

or

def render(implicit m: Manifest[Binding[E]])