@@ -812,4 +812,83 @@ object messages {
812
812
|would give 3 as a result """
813
813
}
814
814
}
815
+
816
+ case class IllegalStartOfSimplePattern ()(implicit ctx : Context ) extends Message (32 ) {
817
+ val kind = " Syntax"
818
+ val msg = " illegal start of simple pattern"
819
+ val explanation = {
820
+ val sipCode =
821
+ """ def f(x: Int, y: Int) = x match {
822
+ | case `y` => ...
823
+ |}
824
+ """
825
+ val constructorPatternsCode =
826
+ """ case class Person(name: String, age: Int)
827
+ |
828
+ |def test(p: Person) = p match {
829
+ | case Person(name, age) => ...
830
+ |}
831
+ """
832
+ val tupplePatternsCode =
833
+ """ def swap(tuple: (String, Int)): (Int, String) = tuple match {
834
+ | case (text, number) => (number, text)
835
+ |}
836
+ """
837
+ val patternSequencesCode =
838
+ """ def getSecondValue(list: List[Int]): Int = list match {
839
+ | case List(_, second, x:_*) => second
840
+ | case _ => 0
841
+ |}"""
842
+ hl """ |Simple patterns can be divided into several groups:
843
+ |- Variable Patterns: ${" case x => ..." }.
844
+ | It matches any value, and binds the variable name to that value.
845
+ | A special case is the wild-card pattern _ which is treated as if it was a fresh
846
+ | variable on each occurrence.
847
+ |
848
+ |- Typed Patterns: ${" case x: Int => ..." } or ${" case _: Int => ..." }.
849
+ | This pattern matches any value matched by the specified type; it binds the variable
850
+ | name to that value.
851
+ |
852
+ |- Literal Patterns: ${" case 123 => ..." } or ${" case 'A' => ..." }.
853
+ | This type of pattern matches any value that is equal to the specified literal.
854
+ |
855
+ |- Stable Identifier Patterns:
856
+ |
857
+ | $sipCode
858
+ |
859
+ | the match succeeds only if the x argument and the y argument of f are equal.
860
+ |
861
+ |- Constructor Patterns:
862
+ |
863
+ | $constructorPatternsCode
864
+ |
865
+ | The pattern binds all object's fields to the variable names (name and age, in this
866
+ | case).
867
+ |
868
+ |- Tuple Patterns:
869
+ |
870
+ | $tupplePatternsCode
871
+ |
872
+ | Calling:
873
+ |
874
+ | ${""" swap(("Luftballons", 99)""" }
875
+ |
876
+ | would give ${""" (99, "Luftballons")""" } as a result.
877
+ |
878
+ |- Pattern Sequences:
879
+ |
880
+ | $patternSequencesCode
881
+ |
882
+ | Calling:
883
+ |
884
+ | ${" getSecondValue(List(1, 10, 2))" }
885
+ |
886
+ | would give 10 as a result.
887
+ | This pattern is possible because a companion object for the List class has a method
888
+ | with the following signature:
889
+ |
890
+ | ${" def unapplySeq[A](x: List[A]): Some[List[A]]" }
891
+ | """
892
+ }
893
+ }
815
894
}
0 commit comments