I want to pattern match items in a tuple, where the items have the same type, including the type parameter. My use-case is fairly complex, but I've tried to extract a minimal example here to illustrate the problem:
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
}
But this doesn't compile, because I'm using x
in the case statement twice. The compile gives the following error:
error: x is already defined as type x
I've tried changing the match to pull out different type parameters for the two inputs and then testing their equality:
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
}
But that doesn't compile, giving the error
error: not found: value x
Is there some Scala syntax that I can use to match two things as having the same, unspecified, type? I am running Scala 2.9.2