About covariance :
Since Manifest[A] is invariant with respect to the parameter A , you cannot do what you want directly. The usual strategy is to weaken the return type,
trait SomeTraitOf[+A] { def newInstance[B >: A](implicit m: Manifest[B]): B = { m.erasure.newInstance.asInstanceOf[B] } }
You can use the attribute as follows:
class Parent class Child extends Parent val childBuilder = new SomeTraitOf[Child] {} val parentBuilder: SomeTraitOf[Parent] = childBuilder parentBuilder.newInstance
About viewing borders :
From your comment below, I think you are also asking about โview restrictionsโ, which are just a brief way of declaring an implicit parameter. Your ad
class SomeTraitOf[A : Manifest] { ...
basically converted to
class SomeTraitOf[A]()(implicit m0: Manifest[A]) { ....
Traits cannot have boundaries of the form, because they cannot take any (values) parameters. But this is not a problem because in your example
class SomeTraitOf[A : Manifest] { def newInstanceOfA(implicit m : Manifest[A]) : A = m.erasure.newInstance.asInstanceOf[A] }
You are not using a view restriction! (Instead, you use the m parameter.) If you want to use view binding, you can do it like this:
class SomeTraitOf[A : Manifest] { def newInstanceOfA : A = implicitly[Manifest[A]].erasure.newInstance.asInstanceOf[A] }
source share