2011-12-15 120 views

回答

2

这是否不符合您的需求?

class A[T](ts: Seq[T]) 
+0

这就是我试图移动远离。这就是我当前的实现如何工作,但我想将参数化类型视为对我需要做的很多通用实例(例如:List [String],List [Int],List [NyanCat])有用。等等)而不是通常少于10个用例(例如:TV [NTSC],TV [PAL],TV [Digital])的东西。 – duckworthd 2011-12-15 04:27:34

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} = [email protected] 

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] = [email protected] 

scala> new AA[String](0) 
<console>:10: error: type mismatch;  
    found   : Int(0)  
    required: String              
     new AA[String](0)              
                    ^ 
+0

我不确定这种类型的理论..你可以解释一下如何实现OP的目标吗? – 2011-12-15 02:34:10