在覆盖方法中使用和扩展类型
Using and extended type in an overriden method
我正在尝试创建一个抽象 class,它有一个方法,我想采用与某些基础 class 相同或扩展的参数。我以为会是:
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(我怀疑 A 也需要更改)我如何将 datum
类型定义为 ExtendedInputParams
for (new B).input(some-datum-of-type-ExtendedInputParams)
以便覆盖 input
可以将 datum
视为 ExtendedInputParams
并仍然强制其扩展 InputParams
?我还想强制该方法覆盖 class A
中的抽象方法。
为清楚起见进行了更新
缩小子类中的约束:
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
改写:
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
我正在尝试创建一个抽象 class,它有一个方法,我想采用与某些基础 class 相同或扩展的参数。我以为会是:
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(我怀疑 A 也需要更改)我如何将 datum
类型定义为 ExtendedInputParams
for (new B).input(some-datum-of-type-ExtendedInputParams)
以便覆盖 input
可以将 datum
视为 ExtendedInputParams
并仍然强制其扩展 InputParams
?我还想强制该方法覆盖 class A
中的抽象方法。
为清楚起见进行了更新
缩小子类中的约束:
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
改写:
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