title: scala class names in configuration map
body: <p>I have a web application built on play framework.</p>
  
  <p>One of my endpoints provides response composed of output of different classes. The consumer can choose which pieces of output he wants to have in the response:
  <code>GET /model/:modelId?fields=length,width,age</code>.
  Accordingly, I have <code>LengthProvider</code>, <code>WidthProvider</code>.
  Now, I want to have a configuration map in the controller, like</p>
  
  <pre><code>val providers = Map(
      "length" -&gt; LengthProvider,
      "width"  -&gt; WidthProvider
  )
  </code></pre>
  
  <p>to be able to filter consumer's input by this map and apply all the providers, like </p>
  
  <pre><code>fields.collect {
      case field if providers.contains(field) =&gt; providers(field)(db).get(modelId)
    }
  </code></pre>
  
  <p>The problem arises when I try to build corresponding classes.
  I have an abstract <code>FieldProvider</code>:</p>
  
  <pre><code>abstract class FieldProvider(db: DB) {
    def get(modelId: Long): Future[Seq[Writes]]
  }
  </code></pre>
  
  <p>And corresponding implementations (<code>LengthProvider</code>, <code>WidthProvider</code>). Now, to make the configuration Map compile, I need to define companion objects for the classes I want to reference (e.g. I need <code>LengthProvider</code> object). And then, when I use the value of the Map (class name), I cannot initialize the class instance like I do that normally, I only have access to the <code>apply</code> method defined in companion object. But the problem is that this is not part of the abstract class I define, so the compiler doesn't understand the <code>apply</code> method signature (as it is only defined in subclasses' companion objects, not in the parent abstract class):</p>
  
  <pre><code>fields.collect {
      case field if providers.contains(field) =&gt; providers(field)(db).get(modelId)
                                                                  ↑ won't compile
    }
  </code></pre>
  
  <p>Is there a way to make companion object's <code>apply</code> signature part of the abstract class? Or is there an another, better approach?</p>