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?