title: Is it possible to use a typeclass to implement a trait?
body: <p>I have a situation where I'd like to implement a given trait (<code>CanBeString</code> in the example below). I would like to have the option either to implement that trait using a newly created case class (<code>NewImplementation</code> in the example below), or to implement it by adding functionality to some pre-existing type (just <code>Int</code> in the example below), by using a type class. This is probably best illustrated by the below:</p>
  <pre><code>package example
  
  // typeclass
  trait ConvertsToString[A] {
    def asString(value: A): String
  }
  
  // the trait I would like the typeclass to implement
  trait CanBeString {
    def asString: String
  }
  
  // this implementation approach taken from the scala with cats book
  object ConvertsToStringInstances {
    implicit val intConvertsToString: ConvertsToString[Int] = 
      new ConvertsToString[Int] {
        def asString(value: Int): String = s&quot;${value}&quot;
      }
  }
  
  object ConvertsToStringSyntax {
    implicit class ConvertsToStringOps[A](value: A) {
      def asString(implicit c: ConvertsToString[A]): String = c.asString(value)
    }
  }
  
  object Test {
    import ConvertsToStringInstances._
    import ConvertsToStringSyntax._
  
    def testAsFunc(c: CanBeString): String = c.asString
  
    case class NewImplementation (f: Double) extends CanBeString {
      def asString = s&quot;{f}&quot;
    }
  
    println(testAsFunc(NewImplementation(1.002))) // this works fine!
    println(testAsFunc(1)) // this sadly does not.
  }
  </code></pre>
  <p>Is anything like this possible? I'm only recently discovering the topic of typeclasses so I'm aware that what I'm asking for here may be possible but just unwise - if so please chime in and let me know what a better idiom might be.</p>
  <p>Thanks in advance, and also afterwards!</p>