2011-12-15 146 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