【发布时间】:2011-07-31 22:44:31
【问题描述】:
由于 scala 的一个特性,对 Manifest 的访问似乎很棘手。
这段代码如何在 scala 中编译?
trait SomeTraitOf[+A] {
def newInstanceOfA : A = /* necessary code to make it work */
}
(相关,它作为参数化类可以正常工作:
class SomeTraitOf[A : Manifest] {
def newInstanceOfA(implicit m : Manifest[A]) : A =
m.erasure.newInstance.asInstanceOf[A]
}
但不使用协变类型参数 (+A))
编辑:真实的东西
sealed trait RootPeerProxy[+A] extends Proxy {
def peer: A
def self = peer
def peerManifest[B >: A](): Option[Manifest[B]]
private[scalavaadin] def newInstance() : Option[A]
}
trait PeerProxy[+A] extends RootPeerProxy[A] {
override def peerManifest[B >: A](): Option[Manifest[B]]
override def peer(): A = this.newInstance match {
case None => {throw new IllegalStateException("oups")}
case Some(a) => a
}
private[scalavaadin] override def newInstance() : Option[A] = peerManifest map { m => m.erasure.newInstance.asInstanceOf[A] }
}
由于特征不能为参数化特征提供清单,实现该特征的类应该,但我不明白。
【问题讨论】: