title: Factory design pattern in Scala with case classes
body: <p>I'm trying to implement a factory design pattern in Scala using the apply methods available on the companion object. I have the following approach.</p>
  
  <pre class="lang-scala prettyprint-override"><code>sealed trait MyType {
    def param: String
  }
  
  case class TypeA(param: String) extends MyType
  case class TypeB(param: String, anotherParam: String) extends MyType 
  
  object MyType {
    def apply(param: String): TypeA = ???
    def apply(param, anotherParam: String): TypeB = ???
  }
  </code></pre>
  
  <p>How do I now force the callers of the above trait to go via the companion object when creating instances of <code>TypeA</code> or <code>TypeB</code>?</p>