The instanciation part can use the manifest : see this SO>
Scala's experimental feature is called manifests, which are a way around the Java restriction regarding type erasure.
class Test[T](implicit m : Manifest[T]) { val testVal = m.erasure.newInstance().asInstanceOf[T] }
In this version you are still writing
class Foo val t = new Test[Foo]
However, if there is no no-arg constructor, you get an exception at run time instead of a static type error
scala> new Test[Set[String]] java.lang.InstantiationException: scala.collection.immutable.Set at java.lang.Class.newInstance0(Class.java:340)
Thus, a true secure solution will use Factory.
Note: as indicated in this thread , Manifest is here to stay, but for now "use only access to erase the type as an instance of the class."
The only thing that appears now is erasing the static type of the parameter on the call site (unlike getClass , which gives you erasing the dynamic type).
Then you can get the method through reflection:
classOf[ClassName].getMethod("main", classOf[Array[String]])
and call him
scala> class A { | def foo_=(foo: Boolean) = "bar" | } defined class A scala>val a = new A a: A = A@1f854bd scala>a.getClass.getMethod(decode("foo_="), classOf[Boolean]).invoke(a, java.lang.Boolean.TRUE) res15: java.lang.Object = bar
VonC Sep 24 '09 at 6:36 2009-09-24 06:36
source share