Search code examples
scalainner-classesimplicitenrich-my-library

Enrich an inner class


I want to implement the enrich-my-library pattern for the inner class that will work for any instance of the outer class. Something like this:

class Outer {
  class Inner(val x: Option[Inner] = None) {
    def test(y: Inner) {}
  }
}

implicit class ExtInner(inner: Outer#Inner) {
  def get = inner.x.get
}

val outer = new Outer
val inner = new outer.Inner(Some(new outer.Inner))
inner.test(inner.get)

This code will not compile, because of the type mismatch: get returns an object of type Outer#Inner but test expects outer.Inner.

I have two ways to make it work but both involve the use of asInstanceOf which I would like to avoid. The first one is simply to cast the result of get to outer.Inner:

inner.test(inner.get.asInstanceOf[outer.Inner]) 

The second is a bit more generic and does the casting in ExtInner:

implicit class ExtInner[T <: Outer#Inner](inner: T) {
  def get = inner.x.get.asInstanceOf[T]
}

Is there a better way to enrich an inner class so there will be no need to do the casting?


Solution

  • How about:

    object Test extends App {
      class Outer {
        class Inner(val x: Option[Inner] = None) {
          def test(y: Inner) { println(s"$toString testing $y") }
        }
        implicit class ExtInner(val inner: Inner) {
          def get = inner.x.get
        }
      }
    
      val outer = new Outer
      val inner = new outer.Inner(Some(new outer.Inner))
      import outer._
      inner.test(inner.get)
    }
    

    or just enhance outer's Inners:

      implicit class ExtInner(val inner: outer.Inner) {
        def get = inner.x.get
      }
    

    Also, I sense that you can invoke the method this way, but it will not infer the singleton type param outer.type in order to summon the implicit.

      implicit class ExtInner[A <: Outer with Singleton](val inner: A#Inner) {
        def get: A#Inner = inner.x.get
      }
      inner.test(ExtInner[outer.type](inner).get)