title: Put method in trait or in case class?
body: <p>There are two ways of defining a method for two different classes inheriting the same trait in Scala.</p>
  
  <pre><code>sealed trait Z { def minus: String }
  case class A() extends Z { def minus = "a" }
  case class B() extends Z { def minus = "b" }
  </code></pre>
  
  <p>The alternative is the following:</p>
  
  <pre><code>sealed trait Z { def minus: String = this match {
      case A() =&gt; "a"
      case B() =&gt; "b"
  }
  case class A() extends Z
  case class B() extends Z
  </code></pre>
  
  <p>The first method repeats the method name, whereas the second method repeats the class name.</p>
  
  <p>I think that the first method is the best to use because the codes are separated. However, I found myself often using the second one for complicated methods, so that adding additional arguments can be done very easily for example like this:</p>
  
  <pre><code>sealed trait Z {
    def minus(word: Boolean = false): String = this match {
      case A() =&gt; if(word) "ant" else "a"
      case B() =&gt; if(word) "boat" else "b"
  }
  case class A() extends Z
  case class B() extends Z
  </code></pre>
  
  <p>What are other differences between those practices? Are there any bugs that are waiting for me if I choose the second approach?</p>
  
  <p><strong>EDIT:</strong>
  I was quoted the open/closed principle, but sometimes, I need to modify not only the output of the functions depending on new case classes, but also the input because of code refactoring. Is there a better pattern than the first one? If I want to add the previous mentioned functionality in the first example, this would yield the ugly code where the input is repeated:</p>
  
  <pre><code>sealed trait Z { def minus(word: Boolean): String  ; def minus = minus(false) }
  case class A() extends Z { def minus(word: Boolean) = if(word) "ant" else "a" }
  case class B() extends Z { def minus(word: Boolean) = if(word) "boat" else "b" }
  </code></pre>