Z Scala Language Specification, punkt 8.1 Wzory, identyfikator po: musi być to, co jest określane jako typ wzoru, zdefiniowane w rozdziale 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.
Tak więc, składnie, nie można używać w pełni kwalifikowanej klasy jako wzorca zmiennej typu W TYM POZYCJI. Możesz jednak użyć aliasu typu, więc:
type JavaInt = java.lang.Integer
List(new java.lang.Integer(5)) match {
case y: Seq[JavaInt] => 6
case _ => 7
}
powróci 6 zgodnie z oczekiwaniami. Problem polega na tym, że jak Alan Burlison zwraca uwagę, poniższe również zwraca 6:
List("foobar") match {
case y: Seq[JavaInt] => 6
case _ => 7
}
, ponieważ typ jest usuwany. Możesz to zobaczyć, uruchamiając REPL, lub skalak z opcją -unchecked.
Wow. Uderzyłem to dzisiaj i ostatecznie znalazłem to pytanie. Nie miałem pojęcia, że w Scali istnieje nawet "wzór zmiennej typu". Co jeszcze ukrywa się przed mną w SLS ...? –
Niektóre z pisanek aktywują się tylko w 1/4 i Wielkanoc. –
Zacząłem wątek na ten temat na https://groups.google.com/d/msg/scala-language/2PNDjkI47Ao/MCQw7RzNUwcJ –