title: Pass type information at runtime
body: <p>I have a DSL and some runtime code. The problem is I got somewhere at runtime:</p>
  
  <pre><code>val clazz: Class[_ &lt;: java.io.Serializable] = classOf[java.lang.String]
  val value: java.io.Serializable = "abc"
  </code></pre>
  
  <p>and I have a class</p>
  
  <pre><code>class MyWrapper[T &lt;: java.io.Serializable](val data: T)(implicit m: Manifest[T]) {
    override def toString = m.runtimeClass
  }
  val wrapper = new MyWrapper(value)
  </code></pre>
  
  <p>The problem is I need to return <code>java.lang.String</code> from a call of <code>toString</code>. But I got <code>java.io.Serializable</code>. Unfortunately I am neither able to create fixed pattern matching for each <code>java.io.Serializable</code> subtype (this would be crazy) nor to create <code>MyWrapper</code> explicit with <code>new MyWrapper[String](value)</code>. I don't know the type of <code>value</code>, maybe it is a subtype of <code>Serializable</code>.</p>
  
  <p>Is there some way to pass type/manifest value at runtime if I know that value type is equal to variable <code>clazz</code>?</p>
  
  <hr>
  
  <p>Update (solution from Régis Jean-Gilles don't work). REPL test:</p>
  
  <pre><code>val clazz: Class[_ &lt;: java.io.Serializable] = classOf[java.lang.String]
  val value: java.io.Serializable = "abc"
  
  class MyWrapper[T &lt;: java.io.Serializable](val data: T)(implicit m: Manifest[T]) {
    override def toString = m.runtimeClass.getName
  }
  val wrapper = new MyWrapper(value)
  //val wrapper = new MyWrapper(value)(ClassManifest.fromClass(clazz).asInstanceOf[ClassTypeManifest[java.io.Serializable]])
  //val wrapper = new MyWrapper(value)(ClassManifest.fromClass(clazz))
  System.err.println(wrapper.toString)
  </code></pre>
  
  <p>I got an error, if I tried to pass manifest explicit:</p>
  
  <pre><code>scala&gt; :load test.scala
  Loading test.scala...
  clazz: Class[_ &lt;: java.io.Serializable] = class java.lang.String
  value: java.io.Serializable = abc
  defined class MyWrapper
  &lt;console&gt;:10: error: type mismatch;
   found   : scala.reflect.ClassManifest[_$1] where type _$1 &lt;: java.io.Serializable
      (which expands to)  scala.reflect.ClassTag[_$1]
   required: Manifest[java.io.Serializable]
         val wrapper = new MyWrapper(value)(ClassManifest.fromClass(clazz))
                                                                   ^
  &lt;console&gt;:8: error: not found: value wrapper
                System.err.println(wrapper.toString)
  </code></pre>
  
  <p>Also I am unable to cast manifest explicit.</p>
  
  <p>There is more strange error when I try to compile my application - </p>
  
  <pre><code>[error]  found   : scala.reflect.ClassManifest[(some other)_0(in method setProperty)]
  [error]     (which expands to)  scala.reflect.ClassTag[(some other)_0(in method setProperty)]
  [error]  required: Manifest[_0(in method setProperty)]
  [error]           new Value.Static(default, Context.virtual(element))(ClassManifest.fromClass(elementProperty.typeClass)))
  </code></pre>
  
  <p>IMHO Régis Jean-Gilles very very close to solution. How to make it work with Scala 2.10?</p>