Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
925 views
in Technique[技术] by (71.8m points)

generics - Scala: "No manifest available for type T"

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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]])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...