I want to be able to pass a parameter of the same type in a function in the outer nested generic class, but also have a polymorphic internal generic class as well - allowing me to use an abstract base class/trait for InnerShell and OuterShell both. Is this even possible in Scala?
class OuterShellClass[Generic] {
def doSomething(param : OuterShellClass[Generic]) : OuterShellClass[Generic] = this;
}
abstract class InnerShellClass[Type] {
}
class InnerShellSubclass[Type] extends InnerShellClass[Type]{
}
class wrapper{
val obj : OuterShellClass[InnerShellClass[String]] = new OuterShellClass[InnerShellSubclass[String]];
}
If the OuterShellClass is a invariant, I get the error:
Note: InnerShellSubclass[String] <: InnerShellClass[String], but class OuterShellClass is invariant in type Generic
If the OuterShellClass is covariant, I get the error:
covariant type Generic occurs in contravariant position in type OuterShellClass[Generic] of value param
Update:
As I am thinking about it, this quick hack will work - please tell me there is a better solution. Since the generics inside OuterShellClass functions are effectively independent of the generic definition at class creation - not necessarily the same class - I cannot guarantee compatible classes at runtime. Move these specific functions to a global function class so I can reclaim the guarantee and get a solution. Do I really need to fragment my classes like this? Is there a better solution?
class OuterShellClass[+Generic] {
}
class Helper {
def doSomething[T] (param1: OuterShellClass[T], param2: OuterShellClass[T]) = param1;
}
abstract class InnerShellClass[Type] {
}
class InnerShellSubclass[Type] extends InnerShellClass[Type]{
}
class wrapper{
val obj : OuterShellClass[InnerShellClass[String]] = new OuterShellClass[InnerShellSubclass[String]];
}