Hi,
In a package whose code is outside my control, there is a hierarchy of classes arranged as follows:
class Outer:
class Inner(number: Int):
???
I'm trying to add a wrapper around Inner as a convenience, but because Inner depends on its enclosing instance, I've only managed to place it within a subclass of that enclosing class:
new Outer:
case class InnerCase(number: Int) extends Inner(number):
???
(new InnerCase(3)).copy() // sample usage
Unfortunately, subclassing Outer in several places means repeating that convenience case class inside each new subclass. To avoid repeatedly duplicating the definition, I'd prefer a class usable with an arbitrary Outer#Inner This is the solution I tried:
case class InnerCase(number: Int)(owner: Outer) extends owner.Inner(number):
???
new Outer:
host =>
InnerCase(3)(host).copy()(host)
new Outer:
host =>
val instance = InnerCase(4)(host).copy()(host)
val widened: Inner = instance // line causing trouble
But using an InnerCase polymorphically in a place requiring an Inner produces this diagnostic:
Found: InnerCase
Required: Inner
I'm still very much a beginner with Scala, so I imagine there must be a better way to achieve what I'm after here. Presumably there's a type-theoretic explanation for why my direct approach violates the type system. Either way, I'd really welcome pointers toward the “proper” approach to subclassing an inner class.