Is is possible to capture the type parameter of a trait using Manifests in Scala 2.7.7?

For now, Scala represents traits as interfaces so this technique will work. There are some problems with this approach to implementing traits, however, in that when methods are added to a trait, the implementing class will not necessarily recompile because the interface representation only has a forwarding method pointing to another class that actually implements the method concretely. In response to this there was talk earlier this year of using interface injection into the JVM at runtime to get around this problem.

If the powers that be use this approach then the trait's type information will be lost before you can capture it.

I could be wrong, but I think that since the Manifest is supplied by the compiler my technique would work regardless of how the trait is implemented. A technique based on runtime reflection, on the other hand, might run into trouble. – Aaron Novstrup Dec 23 '09 at 20:28 Right.

I am guessing this is why all of this is not officially documented right now because the underlying implementation could change on you. – Ichorus Dec 23 '09 at 22:19.

The type information is accessible with the Java reflection API. It's not pretty but it works: trait AT{ def typeParameter = { val genericType = getClass. GetGenericInterfaces()(0).

AsInstanceOfParameterizedType genericType. GetActualTypeArguments()(0) } } class B extends AInt new B(). TypeParameter -> java.lang.

Integer Some invariant checks should be added I've only implemented the happy path.

I was thinking that erasure would make it impossible to use reflection, but I guess not since the test class is not parameterized. Thanks! – Aaron Novstrup Dec 23 '09 at 20:49.

While researching this topic, I found about a solution in this scala-list post by Jorge Ortiz, which did the trick for me, and is simpler than Aaron's. In essence, his solution is (paraphrasing): trait AT { implicit val t: ManifestT } class BT: Manifest extends AT { override val t = manifestT } (I'm ignoring the OP request to be 2.7.7 compatible as I'm writing this in 2011...).

I found a solution that works, but it's pretty awkward since it requires the test class to call a method (clazz) on the trait before any of the trait's lazy vals are evaluated. /** * Utility trait for HttpUnit/ServletUnit tests * * @param T Type parameter for the class under test */ trait ServletUnitTestT Erasure /** * ServletUnit {@link ServletRunner} */ sealed lazy val servletRunner: ServletRunner = { val sr = new ServletRunner(); sr. RegisterServlet(servletName, servletClass.

GetName); sr } /** * A {@link com.meterware.servletunit. ServletUnitClient} */ sealed lazy val servletClient = servletRunner. NewClient /** * The servlet URL, useful for constructing WebRequests */ sealed lazy val servletUrl = "http://localhost/" + servletName def servlet(ic: InvocationContext) = ic.getServlet.

AsInstanceOfT } class MyServletTest extends ServletIUnitTestMyServlet { val servletName = "download" val servletClass = clazz // ... test code ... }.

You do not need to registerServlet if you are going use the default Jasper compiler. However, I needed Jasper jars and their dependencies on the CLASSPATH. I am stuck in a JDK1.4 project,so you may be able to use newer versions.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions