2016-09-30 25 views
0

我正在使用Scala 2.10的項目中工作,並且我使用了擴展或匿名函數的代碼,例如如何擺脫「非變量類型參數鍵入類型模式(類型,類型)未選中,因爲它通過擦除消除」?

multiTest("Tarn to Mc", fixture1) { 
    case (capRaw: (Double, Int), /* .. more arguments .. */ callPut: Tuple2[Double, Double]) => 
    // test body implementation 
} 

上面我這兩種情況下得到警告:

non-variable type argument Double in type pattern (Double, Int) is unchecked since it is eliminated by erasure 

如何才能擺脫這樣的警告,而無需定義自己的類的UDT?

回答

1

嘗試:

multiTest("Tarn to Mc", fixture1) { 
    case ((d: Double, i: Int), /* .. more arguments .. */ callPut: Tuple2[Double, Double]) => 
    // test body implementation 
} 

這可能會刪除編譯警告。

1

儘管Barrameda的回答是完全合理的,但通過允許編譯器自己檢查類型,您通常可以做得更好。它looks like you went with the first solution to your previous question,但如果您使用

case class FixtureTable[A](heading: Map[String, String], values: Seq[A]) 
def multiTest[A](testName: String, fixture: FixtureTable[A])(fun: A => Unit)(implicit pos: source.Position): Unit = ... 

第二個,你不會得到警告,因爲編譯器已經知道capRaw已鍵入(Double, Int)並不需要插入一張支票(當然,對於同樣的原因,你可以只寫case capRaw =>沒有類型)。

+0

哈哈哈良好的Q值間......因爲現在一切正常,我會推遲做你的建議,但我會盡力實施你提出的更清潔的解決方案。但是,正如您在使用模板參數很難實現通用交叉產品之前所指出的那樣。現在我只用'Product'而不用'[A]' –