title: What is the correct way to use typeclass syntax when you have a typeclass within a typeclass?
body: <p>I have a situation where I am using a typeclass-within-a-typeclass in order to overload methods of the original typeclass. Example below:</p>
  <pre><code>abstract class IsArray[A, T: Numeric] {
    def getSingleElem(self: A, idx: Int): T
    def getRef[R](self: A, ref: R)(implicit refTc: RefTC[R]): refTc.Out = refTc.getRef(self, ref)
  
    trait RefTC[R] {
      type Out
      def getRef(self: A, ref: R): Out
    }
    object RefTC {
      implicit val numsTcForSingleInt = new RefTC[Int] {
        type Out = T
        def getRef(self: A, ref: Int): Out = getSingleElem(self, ref)
      }
      implicit val numsTcForListInt = new RefTC[List[Int]] {
        type Out = List[T]
        def getRef(self: A, ref: List[Int]): Out = ref.map(getSingleElem(self, _))
      }
    }
  }
  </code></pre>
  <p>This all works fine. Where I am having difficulties is in creating a 'syntax' object for the typeclass, so the methods can be called directly from values that implement the typeclass. My first attempt looks like this and type-checks OK:</p>
  <pre><code>object IsArraySyntax {
    implicit class IsArrayOps1[A, T: Numeric](self: A)(implicit isArTc: IsArray[A, T]) {
      def getSingleElem(idx: Int): T = isArTc.getSingleElem(self, idx)
      def getRef[R](ref: R)(implicit refTc: isArTc.RefTC[R]): refTc.Out = refTc.getRef(self, ref)
    }
  }
  </code></pre>
  <p>However, I've been getting some strange errors whilst using this (eg <a href="https://stackoverflow.com/questions/63975061/java-lang-nosuchfielderror-when-using-scalatest">java.lang.NoSuchFieldError when using ScalaTest</a>) and I'm wondering if the way I'm writing this is to blame. There is effectively a dependency between the <code>isArTc</code> typeclass and the refTc typeclass, which becomes an explicit dependency if the <code>isArTc</code> typeclass is a parameter of the method rather than than of the <code>IsArrayOps1</code> class, likeso:</p>
  <pre><code>object IsArraySyntax {
    implicit class IsArrayOps2[A, T: Numeric](self: A) {
      def getSingleElem(idx: Int)(implicit isArTc: IsArray[A, T]): T = isArTc.getSingleElem(self, idx)
      def getRef[R](ref: R)(implicit isArTc: IsArray[A, T], refTc: isArTc.RefTC[R]): refTc.Out = refTc.getRef(self, ref)
    }
  }
  </code></pre>
  <p>This does not type check and presumably requires the <code>Aux</code> pattern to be made to work.</p>
  <p>But I was more wondering what is best practice here? Having the <code>isArTc</code> typeclass as a property of the <code>implicit class</code> rather than each method seems to reduce boilerplate and simplify typeclass dependencies, but I've not seen it used and I was wondering if it was undesirable for some other reason? And is <code>refTc: isArTc.RefTC[R]</code> the correct syntax for a typeclass belonging to another typeclass, or should it be something more like <code>refTc: IsArray#RefTC[R]</code>?</p>