title: Support generic deserialization from a List[(String, Any)] in Scala
body: <p>This is a follow up to the following question, which concerned <em>serialization</em>: <a href="https://stackoverflow.com/questions/23897294/how-best-to-keep-a-cached-list-of-member-fields-one-each-for-a-family-of-case-c/23900435?noredirect=1#comment36815879_23900435">How best to keep a cached list of member fields, one each for a family of case classes in Scala</a></p>
  
  <p>I'm trying to generically support deserialization in the same way. One straightforward attempt is the following:</p>
  
  <pre><code>  abstract class Serializer[T](implicit ctag: ClassTag[T]) {
      private val fields = ctag.runtimeClass.getDeclaredFields.toList
      fields foreach { _.setAccessible(true) }
      implicit class AddSerializeMethod(obj: T) {
        def serialize = fields.map(f =&gt; (f.getName, f.get(obj)))
      }
      def deserialize(data: List[(String, Any)]): T = {
        val m = data toMap
        val r: T = ctag.runtimeClass.newInstance // ???
        fields.foreach { case f =&gt; f.set(r, m(f.getName)) }
        r;
      }
    }
  </code></pre>
  
  <p>There are a couple of issues with the code:</p>
  
  <ol>
  <li>The line with <code>val r: T = ...</code> has a compile error because the compiler thinks it's not guaranteed to have the right type. (I'm generally unsure of how to create a new instance of a generic class in a typesafe way -- not sure why this isn't safe since the instance of Serializer is created with a class tag whose type is checked by the compiler).</li>
  <li>The objects I'm creating are expected to be immutable case class objects, which are guaranteed to be fully constructed if created in the usual way. However, since I'm mutating the fields of instances of these objects in the deserialize method, how can I be sure that the objects will not be seen as partially constructed (due to caching and instruction reordering) if they are published to other threads?</li>
  </ol>