Skip to content

poor examples in "pattern expected" explanation with val def, (also do as name causes the problem) #18750

Closed
@bishabosha

Description

@bishabosha

Compiler version

3.3.0

Minimized example

val do = 23

Output Error/Warning message

-- [E032] Syntax Error: --------------------------------------------------------
1 |val do = 23
  |    ^^
  |    pattern expected
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | Simple patterns can be divided into several groups:
  | - Variable Patterns: case x => ....
  |   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.
  |
  | - 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]]
   -----------------------------------------------------------------------------

Why this Error/Warning was not helpful

three things:

  1. I am putting a pattern in a val def, and none of the examples show how to do that, instead only in a match case.
  2. perhaps the user doesn't know that do is a keyword, so this might cause confusion when they expected to define an ordinary value.
  3. the code examples in the explanations are not formatted correctly, so vertical bars are duplicated.

Suggested improvements

  1. maybe conditionally show examples based on the position of the pattern
  2. identify that do is a keyword, so can't be used as a name
  3. fix the formatting of examples.

Metadata

Metadata

Assignees

Labels

area:reportingError reporting including formatting, implicit suggestions, etcbetter-errorsIssues concerned with improving confusing/unhelpful diagnostic messagesitype:enhancement

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions