2
我想模式元組中的匹配項,其中項目具有相同的類型,包括類型參數。我用例是相當複雜的,但我想在這裏提取小例子來說明這個問題:Scala模式多次匹配相同的類型參數
class Foo[T] { }
val input = (new Foo[String], new Foo[String])
input match {
case (a:Foo[x], b:Foo[x]) =>
// Do things that rely on a and b having exactly the same type
}
但是,這並不編譯,因爲我的情況下語句中使用x
兩次。編譯提供了以下錯誤:
error: x is already defined as type x
我試圖改變比賽拉出不同類型參數的兩個輸入端,然後測試他們的平等:
input match {
case (a:Foo[x], b:Foo[y]) if (x == y) =>
// Do things that rely on a and b having exactly the same type
}
但是,這並不編譯,給錯誤
error: not found: value x
有沒有我可以用它來搭配兩件事情具有相同,未指定,類型有些斯卡拉語法?我正在運行Scala 2.9.2
錯誤消息是誤導性的。真正的錯誤是x不是一種類型。 – ben