Dal Scala Language Specification, sezione 8.1 Patterns, l'identificativo dopo il: deve essere quello che viene definito come un tipo di modello, definito nella sezione 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.
Quindi, sintatticamente, non è possibile utilizzare una classe pienamente qualificata come tipo di modello variabile IN QUESTA POSIZIONE. Tuttavia, è possibile utilizzare un alias di tipo:
type JavaInt = java.lang.Integer
List(new java.lang.Integer(5)) match {
case y: Seq[JavaInt] => 6
case _ => 7
}
restituirà 6 come previsto. Il problema è che, come sottolinea Alan Burlison, quanto segue restituisce anche 6:
List("foobar") match {
case y: Seq[JavaInt] => 6
case _ => 7
}
perché il tipo è stato cancellato. Puoi vederlo eseguendo il REPL, o scalac con l'opzione -unchecked.
Wow. L'ho colpito oggi e alla fine ho trovato questa domanda. Non avevo idea che esistesse un tipo di "modello variabile" persino in Scala. Cos'altro mi sta ancora nascondendo in SLS ...? –
Alcune delle uova di Pasqua si attivano solo su 1/4 e Pasqua. –
Ho iniziato una discussione su questo https://groups.google.com/d/msg/scala-language/2PNDjkI47Ao/MCQw7RzNUwcJ –