I have a type-parameterized abstract class that contains a val and a method that both use its type parameter
abstract class Foo[T](val state: T){
def foo(arg: T){
...
}
}
I also have a class that extends this abstract class and provides the type parameter and a value for state
class Bar(myNumber: Int) extends Foo[Int](myNumber){
...
}
I pass an instance of Bar to another class that accepts any subclass of Foo, and I would like to call the method foo on state, but I'm running into some trouble:
class Baz(val f: Foo[_]){
f.foo(f.state)
}
This gives the error:
<console>:8: error: type mismatch;
found : Baz.this.f.state.type (with underlying type _$1)
required: _$1
f.foo(f.state)
Is there any way to give Baz knowledge of Bar's type parameter so that it compiles correctly? Or is that even what I want to do?
Edit
To clarify, I have many classes similar to Bar that extend Foo and provide their own type parameter, state, and implementation of foo. I would like users of my library to be able to pass any of them to Baz without worrying about the type parameter T since its just an implementation detail of each subclass of Foo.
So I would strongly prefer not to do this:
class Baz[T](val f: Foo[T]){
f.foo(f.state)
}