[英]Scala: “No manifest available for type T”
I am working on a Lift project with mixed Scala and Java code. 我正在使用混合Scala和Java代码的Lift项目。
On the Java side, I have the following relevant items: 在Java方面,我有以下相关项目:
interface IEntity
interface IDAO<T extends IEntity> {
void persist(T t);
}
On the Scala side, I have the following: 在Scala方面,我有以下内容:
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:
如果我使用Binding [E]作为参数声明成员函数,则代码编译正常,例如:
def someFunction(binding: Binding[E] = { // same code as above }
Why does this happen, and how can I work around it? 为什么会发生这种情况,我该如何解决这个问题呢?
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]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.