E032: Illegal Start Of Simple Pattern
This error is emitted when the compiler encounters a token that cannot begin a valid pattern in a pattern matching context.
Simple patterns can be divided into several groups:
-
Variable Patterns:
case x => ...orcase _ => ...Matches any value and binds the variable name to that value. The wildcard pattern_is treated as if it was a fresh variable on each occurrence. -
Typed Patterns:
case x: Int => ...orcase _: Int => ...Matches any value matched by the specified type and binds the variable name to that value. -
Given Patterns:
case given ExecutionContext => ...Matches any value matched by the specified type and binds agiveninstance to that value. -
Literal Patterns:
case 123 => ...orcase 'A' => ...Matches any value that is equal to the specified literal. -
Stable Identifier Patterns: Using backticks to match against a variable
case \y` => ...` -
Constructor Patterns:
case Person(name, age) => ...Binds all object's fields to the variable names. -
Tuple Patterns:
case (a, b) => ... -
Pattern Sequences:
case List(first, second, rest*) => ...
Example
def example(x: Any) = x match
case => "none"
Error
-- [E032] Syntax Error: example.scala:2:7 --------------------------------------
2 | case => "none"
| ^^
| 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]]
-----------------------------------------------------------------------------
Solution
// Use a valid pattern - typed pattern
def example(x: Any) = x match
case _: String => "string"
case _ => "other"
// Use a variable pattern
def example(x: Any) = x match
case y => s"value: $y"
// Use a constructor pattern
case class Person(name: String)
def example(x: Any) = x match
case Person(name) => s"person: $name"
case _ => "not a person"