Desde el Scala Language Specification, Sección 8.1 Patrones, el identificador después de la: tiene que ser lo que se conoce como un patrón de tipo, definido en la Sección 8.2:
Type patterns consist of types, type variables, and wildcards. A type pattern T is of one of the following forms:
...
A parameterized type pattern T [a(1), . . . , a(n)], where the a(i) are type variable patterns or wildcards _. This type pattern matches all values which match T for some arbitrary instantiation of the type variables and wildcards. The bounds or alias type of these type variable are determined as described in (§8.3).
...
A type variable pattern is a simple identifier which starts with a lower case letter. However, the predefined primitive type aliases unit, boolean, byte, short, char, int, long, float, and double are not classified as type variable patterns.
Por lo tanto, sintácticamente, no se puede usar una clase totalmente calificada como un patrón variable de tipo EN ESTA POSICIÓN. Sin embargo, puede utilizar un alias de tipo, por lo que:
type JavaInt = java.lang.Integer
List(new java.lang.Integer(5)) match {
case y: Seq[JavaInt] => 6
case _ => 7
}
devolverá 6 como se esperaba. El problema es que como señala Alan Burlison, la siguiente también devuelve 6:
List("foobar") match {
case y: Seq[JavaInt] => 6
case _ => 7
}
porque el tipo se está borrando. Puede ver esto ejecutando el REPL o scalac con la opción -unchecked.
Wow. Llegué a esto hoy y finalmente encontré esta pregunta. No tenía idea de que existiera un "patrón variable de tipo" en Scala. ¿Qué más me sigue ocultando en el SLS ...? –
Algunos de los huevos de Pascua solo se activan el 1/4 y Pascua. –
Comencé un hilo sobre esto en https://groups.google.com/d/msg/scala-language/2PNDjkI47Ao/MCQw7RzNUwcJ –