Reputation: 5702
I'm trying to create an abstract class that has a method that I want to take a param that is the same as or extends some base class. I thought it would be:
trait InputParams
abstract class A() {
def input[T <: InputParams](datum: T) // abstract
...
}
case class ExtendedInputParams() extends InputParams
class B() extends A() {
override def input[T <: InputParams](datum: T)...
// the input(datum) method needs more to be able to treat datum as an
// ExtendedInputParams, how do I do this?
...
}
in class B (I suspect A needs changes too) how do I define the datum
type as ExtendedInputParams
for (new B).input(some-datum-of-type-ExtendedInputParams)
so the overriding input
can treat datum
as an ExtendedInputParams
and still enforce that it is extending InputParams
? I also want to enforce that the method overrides the abstract method in class A
.
updated for clarity
Upvotes: 0
Views: 60
Reputation: 39577
To narrow the constraint in the subclass:
scala> class P ; class Q extends P
defined class P
defined class Q
scala> class A { type T <: P ; def f[X <: T](a: X) = a.toString }
defined class A
scala> class B extends A { type T = Q ; override def f[X <: T](a: X) = a.toString * 2 }
defined class B
scala> (new B).f(new Q)
res0: String = Q@1ea930ebQ@1ea930eb
scala> (new B).f(new P)
<console>:14: error: inferred type arguments [P] do not conform to method f's type parameter bounds [A <: Q]
(new B).f(new P)
^
<console>:14: error: type mismatch;
found : P
required: A
(new B).f(new P)
^
scala> (new A { type T = P }).f(new P)
res3: String = P@4300e240
Reworded:
scala> class P ; class Q extends P { def q = 42 }
defined class P
defined class Q
scala> class X[T <: P] { def f[A <: T](a: A) = a.toString }
defined class X
scala> class Y[T <: Q] extends X[T] { override def f[A <: T](a: A) = a.q.toString }
defined class Y
scala> (new Y).f(new Q)
res0: String = 42
Upvotes: 1