-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
better explain message for 'pattern expected' (#18753)
- Loading branch information
Showing
3 changed files
with
94 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
-- [E032] Syntax Error: tests/neg/i18750-format.scala:4:7 -------------------------------------------------------------- | ||
4 | case # => () // error | ||
| ^ | ||
| pattern expected | ||
|--------------------------------------------------------------------------------------------------------------------- | ||
| Explanation (enabled by `-explain`) | ||
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| Simple patterns can be divided into several groups: | ||
| - Variable Patterns: case x => ... or case _ => ... | ||
| It matches any value, and binds the variable name to that value. | ||
| A special case is the wild-card pattern _ which is treated as if it was a fresh | ||
| variable on each occurrence. | ||
| | ||
| - Typed Patterns: case x: Int => ... or case _: Int => ... | ||
| This pattern matches any value matched by the specified type; it binds the variable | ||
| name to that value. | ||
| | ||
| - Given Patterns: case given ExecutionContext => ... | ||
| This pattern matches any value matched by the specified type; it binds a given | ||
| instance with the same type to that value. | ||
| | ||
| - Literal Patterns: case 123 => ... or case 'A' => ... | ||
| This type of pattern matches any value that is equal to the specified literal. | ||
| | ||
| - Stable Identifier Patterns: | ||
| | ||
| def f(x: Int, y: Int) = x match | ||
| case `y` => ... | ||
| | ||
| the match succeeds only if the x argument and the y argument of f are equal. | ||
| | ||
| - Constructor Patterns: | ||
| | ||
| case class Person(name: String, age: Int) | ||
| | ||
| def test(p: Person) = p match | ||
| case Person(name, age) => ... | ||
| | ||
| The pattern binds all object's fields to the variable names (name and age, in this | ||
| case). | ||
| | ||
| - Tuple Patterns: | ||
| | ||
| def swap(tuple: (String, Int)): (Int, String) = tuple match | ||
| case (text, number) => (number, text) | ||
| | ||
| Calling: | ||
| | ||
| swap(("Luftballons", 99)) | ||
| | ||
| would give (99, "Luftballons") as a result. | ||
| | ||
| - Pattern Sequences: | ||
| | ||
| def getSecondValue(list: List[Int]): Int = list match | ||
| case List(_, second, x*) => second | ||
| case _ => 0 | ||
| | ||
| Calling: | ||
| | ||
| getSecondValue(List(1, 10, 2)) | ||
| | ||
| would give 10 as a result. | ||
| This pattern is possible because a companion object for the List class has a method | ||
| with the following signature: | ||
| | ||
| def unapplySeq[A](x: List[A]): Some[List[A]] | ||
--------------------------------------------------------------------------------------------------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
//> using options -explain | ||
|
||
def test = 23 match | ||
case # => () // error |