title: Scala - Typeclass overriding the abstract method
body: <p>I am struggling a little with scala 2.12:</p>
  <p>I have the following hierarchy:</p>
  <pre><code>trait A
  
  case class B(format: String) extends A
  
  trait Writer {
    def write(config: A): Unit
  }
  
  
  val writer = new Writer {
    override def write(config: A) = println(&quot;hi&quot;)
  }
  
  val w = B(&quot;console&quot;)
  writer.write(w)
  </code></pre>
  <p>which works fine. But I want to provide an alternate implementation for writer:</p>
  <pre><code>val writer = new Writer {
    override def write(config: B) = println(&quot;hi&quot;)
  }
  </code></pre>
  <p>But I get <code>object creation impossible, since method write in trait Writer of type (config: Playground.A)Unit is not defined</code></p>
  <p>I assumed that since B is an A, this should work. How can I override <code>write</code> with a config of type <code>B</code> where <code>B &lt;: A</code></p>
  <p>Scastie: <a href="https://scastie.scala-lang.org/QBaiiDP4Sj2lptUjrWLJYw" rel="nofollow noreferrer">https://scastie.scala-lang.org/QBaiiDP4Sj2lptUjrWLJYw</a></p>
  <p><strong>EDIT: ------------------------------------------------------------</strong></p>
  <p>Based on some inputs, I changed the implementation to:</p>
  <pre><code>sealed trait A
  
  case class B(format: String) extends A
  
  trait Writer[+T] {
    def write[S &gt;: T](config: S): Unit
  }
  
  
  val writer: Writer[A] = new Writer[B] {
    override def write[B](config: B) = println(&quot;hi&quot;)
  }
  
  
  val b = B(&quot;console&quot;)
  writer.write(b)
  
  </code></pre>
  <p>which works.</p>
  <p>But if I modify it to access the variables in <code>config</code>, it breaks:</p>
  <pre><code>sealed trait A
  
  case class B(format: String) extends A
  
  trait Writer[+T] {
    def write[S &gt;: T](config: S): Unit
  }
  
  
  val writer: Writer[A] = new Writer[B] {
    override def write[B](config: B) = println(config.format)
  }
  
  
  val b = B(&quot;console&quot;)
  writer.write(b)
  
  </code></pre>
  <p>with <code>value format is not a member of type parameter B</code></p>
  <p><a href="https://scastie.scala-lang.org/Xj2rKbbiTmG7raZgQZYfHA" rel="nofollow noreferrer">https://scastie.scala-lang.org/Xj2rKbbiTmG7raZgQZYfHA</a></p>
  <p>Appreciate the inputs.</p>