title: Scala - Dependency injection in companion objects
body: <p>I am from Java background and new to functional paradigm as such, so please forgive and correct me I smell like OOP.</p>
  
  <p>I have a trait names PaymentHandler </p>
  
  <pre><code>trait PaymentHandler {
  
  def handleInit(paymentInfo: PaymentInfo, paymentConfig: PaymentConfig): Either[Future[WSResponse], Exception]
  
  def handleComplete(paymentinfo: PaymentInfo, paymentConfig: PaymentConfig, clientToken: String): Future[Map[String, String]]
  
  }
  </code></pre>
  
  <p>and </p>
  
  <pre><code>@Singleton
  class PayPal extends PaymentHandler{....}
  @Singleton
  class BrainTree extends PaymentHandler{....}
  </code></pre>
  
  <p>Till here everything is Ok. but the problem comes when I want to have multiple PaymentGateways based on Payment method. 
  Initially I wrote a PaymentHandlerFactory like </p>
  
  <pre><code>class PaymentHandlerFactory @Inject()(
      paypal:PayPal,
      braintree:BrainTree) {
  
    def get(name:String): PaymentHandler = {
       name match {
         case "paypal" =&gt; paypal
         case "bt" =&gt; braintree
         case _ =&gt; null
       }
    }
  }
  </code></pre>
  
  <p>and then use this PaymentHandlerFactory to get the PayPal object injected by guice. But somehow I felt this is not right way to do and I got onto companion objects. so this is the code I wrote then</p>
  
  <pre><code>object PaymentHandler {
    @Inject
    val paypal:PayPal = null
  
    def apply(name:String): PaymentHandler = {
      name match {
        case "paypal" =&gt; paypal
        case _ =&gt; null
      }
    }
  }
  </code></pre>
  
  <p>Ofcourse this fails as it cannot Inject PayPal object. Now I have two questions in my mind. </p>
  
  <ol>
  <li>Why we cannot inject in companion objects in scala?</li>
  <li>What is the right way to implement such factory like things in scala?</li>
  </ol>