Creating Instances of a Limited Type Using Manifests

I am trying to use Scala manifests to instantiate a type, and I am having problems when this type is parameterized in types with view binding. I redid the problem to the following code:

class foo[X <% Ordered[X]]() {}

def boo[T](implicit m : Manifest[T]) = { m.erasure.newInstance().asInstanceOf[T] }

boo[foo[String]]

java.lang.InstantiationException: foo
    at java.lang.Class.newInstance0(Class.java:357)
    at java.lang.Class.newInstance(Class.java:325)
    . . .

So you can see that we have a simple class foo that is parameterized on X; which is limited to a limited order [X]. The boo function simply tries to instantiate foo [String] using manifests. However, when this function is called, everything goes horribly, and I get a stack trace that starts, as I showed. When a parameter of type foo is not limited, an instance is created without problems. I suppose this is because the essence of the view is just syntactic sugar for the existence of the implicit X => Ordered [X] transform, and that somehow the manifest, depending on another manifest, is causing the problem. However, I have no idea what is really happening or, more importantly, how to fix it. Is this possible in Scala, and if not, how do people achieve something similar?

+3
2

newInstance , T . foo . <% ( :) .

class foo[X <% Ordered[X]] class foo(implicit freshName: X => Ordered[X]). foo, newInstance .

+7

:

def boo[A, B[_]](implicit toOrdA: A => Ordered[A], mf: Manifest[B[A]]): B[A] = {
  val constructor = mf.erasure.getConstructor(classOf[A => Ordered[A]])
  constructor.newInstance(toOrdA).asInstanceOf[B[A]]
}
+5

All Articles