很简单,有没有办法结合构造函数参数和抽象类型?例如,我想做的事情
class A(t: Seq[T]) {
type T
}
答案 0 :(得分:6)
该类的成员不在构造函数的参数声明中。
尽可能接近:
scala> trait T { type T; val a: T }
defined trait T
scala> def A[X](x: X) = new T { type T = X; val a = x }
A: [X](x: X)Object with T{type T = X}
scala> A[Int](0)
res0: Object with T{type T = Int} = $anon$1@3bd29ee4
scala> A[String](0)
<console>:10: error: type mismatch;
found : Int(0)
required: String
A[String](0)
^
scala> class AA[X](val a: X) extends T { type T = X }
defined class AA
scala> new AA[Int](0)
res5: AA[Int] = AA@1b3d4787
scala> new AA[String](0)
<console>:10: error: type mismatch;
found : Int(0)
required: String
new AA[String](0)
^
答案 1 :(得分:2)
这不符合您的需求吗?
class A[T](ts: Seq[T])